Java Code Examples for com.google.firebase.messaging.RemoteMessage#Notification

The following examples show how to use com.google.firebase.messaging.RemoteMessage#Notification . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MyFirebaseMessagingService.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 8 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage message) {
  RemoteMessage.Notification notification = message.getNotification();
  if (notification != null) {
    String title = notification.getTitle();
    String body = notification.getBody();
    // Post your own notification using NotificationCompat.Builder
    // or send the information to your UI
  }

  // Listing 11-31: Receiving data using the Firebase Messaging Service
  Map<String,String> data = message.getData();
  if (data != null) {
    String value = data.get("your_key");
    // Post your own Notification using NotificationCompat.Builder
    // or send the information to your UI
  }
}
 
Example 2
Source File: MyFirebaseMessagingService.java    From stitch-examples with Apache License 2.0 6 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage message) {
    RemoteMessage.Notification notification = message.getNotification();
    if (notification == null) { return; }

    // By default, notifications will only appear if the app is in the background.
    // Create a new notification here to show in case the app is in the foreground.
    Notification.Builder notificationBuilder;
    NotificationManager manager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= 26) {
        notificationBuilder = new Notification.Builder(getApplicationContext(), "channel_id");
    } else {
        notificationBuilder = new Notification.Builder(getApplicationContext());
    }

    notificationBuilder.setContentTitle(notification.getTitle())
            .setContentText(notification.getBody())
            .setSmallIcon(R.mipmap.ic_launcher);

    notificationBuilder.setAutoCancel(true)
            .setDefaults(NotificationCompat.DEFAULT_ALL);

    manager.notify(1, notificationBuilder.build());
}
 
Example 3
Source File: FirebaseMessagingPluginService.java    From cordova-plugin-firebase-messaging with MIT License 6 votes vote down vote up
private void showAlert(RemoteMessage.Notification notification) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getNotificationChannel(notification))
            .setSound(getNotificationSound(notification.getSound()))
            .setContentTitle(notification.getTitle())
            .setContentText(notification.getBody())
            .setGroup(notification.getTag())
            .setSmallIcon(defaultNotificationIcon)
            .setColor(defaultNotificationColor)
            // must set priority to make sure forceShow works properly
            .setPriority(1);

    notificationManager.notify(0, builder.build());
    // dismiss notification to hide icon from status bar automatically
    new Handler(getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
            notificationManager.cancel(0);
        }
    }, 3000);
}
 
Example 4
Source File: FirebaseMessagingPlugin.java    From cordova-plugin-firebase-messaging with MIT License 6 votes vote down vote up
static void sendNotification(RemoteMessage remoteMessage) {
    JSONObject notificationData = new JSONObject(remoteMessage.getData());
    RemoteMessage.Notification notification = remoteMessage.getNotification();
    try {
        if (notification != null) {
            notificationData.put("gcm", toJSON(notification));
        }
        notificationData.put("google.message_id", remoteMessage.getMessageId());
        notificationData.put("google.sent_time", remoteMessage.getSentTime());

        if (instance != null) {
            CallbackContext callbackContext = instance.isBackground ?
                instance.backgroundCallback : instance.foregroundCallback;
            instance.sendNotification(notificationData, callbackContext);
        }
    } catch (JSONException e) {
        Log.e(TAG, "sendNotification", e);
    }
}
 
Example 5
Source File: FBaseMessagingService.java    From tindroid with Apache License 2.0 6 votes vote down vote up
private NotificationCompat.Builder composeNotification(@NonNull RemoteMessage.Notification remote) {
    @SuppressWarnings("deprecation") NotificationCompat.Builder notificationBuilder =
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
                    new NotificationCompat.Builder(this, "new_message") :
                    new NotificationCompat.Builder(this);

    final Resources res = getResources();
    final String packageName = getPackageName();

    return notificationBuilder
            .setPriority(unwrapInteger(remote.getNotificationPriority(), NotificationCompat.PRIORITY_HIGH))
            .setVisibility(unwrapInteger(remote.getVisibility(), NotificationCompat.VISIBILITY_PRIVATE))
            .setSmallIcon(resourceId(res, remote.getIcon(), R.drawable.ic_logo_push, "drawable", packageName))
            .setColor(unwrapColor(remote.getColor(), ContextCompat.getColor(this, R.color.colorNotificationBackground)))
            .setContentTitle(locText(res, remote.getTitleLocalizationKey(), remote.getBodyLocalizationArgs(),
                    remote.getTitle(), packageName))
            .setContentText(locText(res, remote.getBodyLocalizationKey(), remote.getBodyLocalizationArgs(),
                    remote.getBody(), packageName))
            .setAutoCancel(true)
            // TODO: use remote.getSound() instead of default.
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
}
 
Example 6
Source File: MyMessagingService.java    From Pharmacy-Android with GNU General Public License v3.0 6 votes vote down vote up
private void createOrderUpdateNotification(RemoteMessage.Notification notification, Map<String, String> data) {

        Context context = getBaseContext();
        String orderId = data.get("order_id");
        String type = data.get("type");

        Intent orderDetailIntent = OrderDetailActivity.getInstanceByOrderId(context, orderId);
        int requestID = (int) System.currentTimeMillis(); //unique requestID to differentiate between various notification with same NotifId
        int flags = PendingIntent.FLAG_CANCEL_CURRENT; // cancel old intent and create new one
        PendingIntent pIntent = PendingIntent.getActivity(this, requestID, orderDetailIntent, flags);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(notification.getTitle())
                .setContentText(notification.getBody())
                .setSound(alarmSound)
                .setColor(ContextCompat.getColor(context, R.color.colorPrimary))
                .setContentIntent(pIntent);

        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(orderId,ORDER_UPDATE_NOTIFICATION_ID, mBuilder.build());
    }
 
Example 7
Source File: FirebaseMessagingPluginService.java    From cordova-plugin-firebase-messaging with MIT License 5 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    FirebaseMessagingPlugin.sendNotification(remoteMessage);

    Intent intent = new Intent(ACTION_FCM_MESSAGE);
    intent.putExtra(EXTRA_FCM_MESSAGE, remoteMessage);
    broadcastManager.sendBroadcast(intent);

    if (FirebaseMessagingPlugin.isForceShow()) {
        RemoteMessage.Notification notification = remoteMessage.getNotification();
        if (notification != null) {
            showAlert(notification);
        }
    }
}
 
Example 8
Source File: FirebaseMessagingPluginService.java    From cordova-plugin-firebase-messaging with MIT License 5 votes vote down vote up
private String getNotificationChannel(RemoteMessage.Notification notification) {
    String channel = notification.getChannelId();
    if (channel == null) {
        return defaultNotificationChannel;
    } else {
        return channel;
    }
}
 
Example 9
Source File: FirebaseMessagingPlugin.java    From cordova-plugin-firebase-messaging with MIT License 5 votes vote down vote up
private static JSONObject toJSON(RemoteMessage.Notification notification) throws JSONException {
    return new JSONObject()
        .put("body", notification.getBody())
        .put("title", notification.getTitle())
        .put("sound", notification.getSound())
        .put("icon", notification.getIcon())
        .put("tag", notification.getTag())
        .put("color", notification.getColor())
        .put("clickAction", notification.getClickAction());
}
 
Example 10
Source File: MessageActivity.java    From tindroid with Apache License 2.0 5 votes vote down vote up
private String readTopicNameFromIntent(Intent intent) {
    // Check if the activity was launched by internally-generated intent.
    String name = intent.getStringExtra("topic");
    if (!TextUtils.isEmpty(name)) {
        return name;
    }

    // Check if activity was launched from a background push notification.
    RemoteMessage msg = intent.getParcelableExtra("msg");
    if (msg != null) {
        RemoteMessage.Notification notification = msg.getNotification();
        if (notification != null) {
            return notification.getTag();
        }
    }

    if (TextUtils.isEmpty(name)) {
        // mTopicName is empty, so this is an external intent
        Uri contactUri = intent.getData();
        if (contactUri != null) {
            Cursor cursor = getContentResolver().query(contactUri,
                    new String[]{Utils.DATA_PID}, null, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    name = cursor.getString(cursor.getColumnIndex(Utils.DATA_PID));
                }
                cursor.close();
            }
        }
    }

    return name;
}
 
Example 11
Source File: MyMessagingService.java    From Pharmacy-Android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Timber.d("From %s", remoteMessage.getFrom());
    Timber.d("Notification Message Body: %s", remoteMessage.getNotification().getBody());

    // TODO: 22/5/16 Show notification on Order update

    Map<String, String> data = remoteMessage.getData();
    RemoteMessage.Notification notification = remoteMessage.getNotification();

    if (data.get("type").equals("ORDER_UPDATE")) {
        createOrderUpdateNotification(notification, data);
    }


}