0

My question is, onMessageReceived event is only being called when app is in foreground and on in that case i am able to trigger my desired notification with desired style. Is there any way to check in which event listener when app is in background and I can trigger the same styled notification instead of default notification.Thanks.

Below is my Service Class

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

/**
 * Called when message is received.
 *
 * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
 */
// [START receive_message]
@Override
public void onMessageReceived(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, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"));
}
// [END receive_message]

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 */
private void sendNotification(String title,String messageBody) {
//                     Toast.makeText(this.getApplicationContext(),messageBody,Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);
    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(getNotificationIcon())
            .setContentTitle(getResources().getString(R.string.app_name))
            .setContentText(messageBody)
            .setLargeIcon(largeIcon)
            .setDefaults(Notification.DEFAULT_ALL)
            .setAutoCancel(false)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(messageBody))
            .setSound(defaultSoundUri)
            .setLights(Color.RED, 300, 300)
            .setVibrate(new long[] { 100, 250 })
            .setContentIntent(pendingIntent);

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

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

}

Here is menifest code for both services

<service android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    <service android:name=".MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>
Ali Akram
  • 199
  • 4
  • 12
  • post manifest please – Tim Jun 14 '16 at 08:32
  • this link might help you >> http://stackoverflow.com/a/37395785/3955965 – mgcaguioa Jun 14 '16 at 08:42
  • @mgcaguioa Thanks for pointing me there, basically I have checked that post it tells about how to receive the payload data when app is in background but question is still there how to control default notification being received when app is in background. – Ali Akram Jun 14 '16 at 08:45
  • My basic question is when app is in background which event listener is triggered to show the default notification so i modify it and show it in my desired style. – Ali Akram Jun 14 '16 at 08:47
  • FirebaseMessagingService's onCreate() is always triggered whenever your app receives push notification but it doesn't have the data (RemoteMessage) – mgcaguioa Jun 14 '16 at 09:01
  • you are right. when app is in background no data is received from server also no vibration, no sound applies on notification appearing.Really strange why firebase has this kind of architecture. – Ali Akram Jun 14 '16 at 09:11
  • @AliAkram think i'w used the same implementation (firebase quck start?), and its working like a charm, maybe it can device specific, what kind of device are you testing this on? – Ivan Milisavljevic Jun 14 '16 at 09:51
  • its an android phone with OS version 5.1, but i am sure its not device specific issue. – Ali Akram Jun 14 '16 at 10:02

0 Answers0