1

I am sending push notification from firebase to my Android Application. but when my app is in background firebase onMessageReceived method is not called instead firebase send notification to system for showing notification in system tray. notification appears in system tray but no sound for notification even i have allowed notification sound for my app in system settings. this my code for notification thank you

    private void sendNotification(String msg, String title) {
    NotificationManager mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class), 0);
    Intent i = new Intent(this,MainActivity.class);
    i.putExtra("","");

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(title)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                    .setContentText(msg);
    //Vibration
    mBuilder.setVibrate(new long[] { 200, 400, 600, 800, 1000 });

    //LED
    mBuilder.setLights(Color.MAGENTA, 1000, 1000);

    //Ton
    mBuilder.setSound(Uri.parse("android.resource://"
            + getApplicationContext().getPackageName() + "/" + R.raw.circles_notif));
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(0, mBuilder.build());
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Med Elgarnaoui
  • 1,612
  • 1
  • 18
  • 35
  • Have you defined the android permission for using vibrate in the AndroidManifest?: `` – GeorgeLBA Sep 06 '16 at 15:02
  • yes this is not my problem when i click buton to test its work but in background not working – Med Elgarnaoui Sep 06 '16 at 15:40
  • Have you tried using the `setDefaults(DEFAULT_ALL)` from the NotificationCompat to check if it works with the user's default preferences? and remove the vibration, light and sound that you defined just for testing. Hope this helps – GeorgeLBA Sep 06 '16 at 16:03
  • yes i tried that but the same problem – Med Elgarnaoui Sep 06 '16 at 16:05
  • I have an application with a notification working with vibrate. The only difference with your code is that I have the code like this: `new NotificationCompat.Builder(this).setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).setVibrate(new long[]{400,500,300});` not defined from the `mBuilder`. – GeorgeLBA Sep 06 '16 at 16:10
  • my app work in forground but in background i recieve just a notif without sound and vibrate and without custom icon – Med Elgarnaoui Sep 06 '16 at 16:15
  • hello @GeorgeLBA can you share with me your FcmMessagingService class and the json sending from server plz – Med Elgarnaoui Sep 07 '16 at 09:15
  • Could you include in the question how you are sending the notification? Are you using HTTP API or the Firebase console? – Arthur Thompson Sep 07 '16 at 13:44
  • i am using http api – Med Elgarnaoui Sep 07 '16 at 14:13

3 Answers3

3

Go to Notifications> New Message> Advanced Options from your firebase console and send notification as Custom data. Use 'title' and 'body' as Key and put their values as you want to show them in notification. This should call onMessageReceived() when your app is in background or killed.

If you are sending notification from your own server, here is a sample json part:

{   
    "data": {
      "title": "notification_title",
      "body": "notification_body"
    },
    "to" : "jh578_gsh....jhHGFJ76FH"
}
Sudip Podder
  • 830
  • 11
  • 25
  • hello @sudip Podder when the app in forground i want do not recieve a notif – Med Elgarnaoui Sep 07 '16 at 14:10
  • I dont have much practical experience about it, but you may follow this link: http://stackoverflow.com/questions/5504632/how-can-i-tell-if-android-app-is-running-in-the-foreground. Looks like It may provide the solution you are looking for. – Sudip Podder Sep 07 '16 at 14:23
2

It's written in the override sample of onMessageReceived(), the second comment line says:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    ...
    Not getting messages here? See why this may be: ...goo.gl/39bRNJ
    ...
}

see comment-link

The solution, like the answers for the related question: How to handle notification when app in background in Firebase, can be found in the documentation in Messages with both notification and data payloads section which says:

App behavior when receiving messages that include both notification and data payloads depends on whether the app is in the background or the foreground—essentially, whether or not it is active at the time of receipt.

  • When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
  • When in the foreground, your app receives a message object with both payloads available.
Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99
1

This seem to work for me YMMV

{
    "notification": {
        "title": "notification_title",
        "body": "notification_body",
        "sound": "default"
    },
    "to" : "device....id"
}

While this will not wake up your app if background while the accepted answer will.

Samuel
  • 9,883
  • 5
  • 45
  • 57