Skip to content Skip to sidebar Skip to footer

Firebase Notification And Data

I am Creating an App using Firebase cloud messaging API... I am able to send notification and data to my client application from the server. But the problem is When the application

Solution 1:

When you send a notification message with a data payload (notification and data) and the app is in the background you can retrieve the data from the extras of the intent that is launched as a result of the user tapping on the notification.

From the FCM sample which launches the MainActivity when the notification is tapped:

if (getIntent().getExtras() != null) {
    for (String key : getIntent().getExtras().keySet()) {
        Object value = getIntent().getExtras().get(key);
        Log.d(TAG, "Key: " + key + " Value: " + value);
    }
}

Solution 2:

FCM will not show notification if your app is in foreground, So you have manually show that like this:-

@OverridepublicvoidonMessageReceived(RemoteMessage remoteMessage) {
        // TODO(developer): Handle FCM messages here.// If the application is in the foreground handle both data and notification messages here.// Also if you intend on generating your own notifications as a result of a received FCM// message, here is where that should be initiated. See sendNotification method below.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "From: " + remoteMessage.getData().get("message"));
//        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
        sendNotification(remoteMessage.getData().get("message"));
    }
    // [END receive_message]/**
     * Create and show a simple notification containing the received FCM message.
     *
     * @param messageBody FCM message body received.
     */privatevoidsendNotification(String messageBody) {
        Intentintent=newIntent(this, MarketActivity.class);
//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);PendingIntentpendingIntent= PendingIntent.getActivity(this, 0/* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.BuildernotificationBuilder=newNotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Fred Notification")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentIntent(pendingIntent);

        NotificationManagernotificationManager=
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0/* ID of notification */, notificationBuilder.build());
    }

Post a Comment for "Firebase Notification And Data"