1
votes

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());
}
3
Have you defined the android permission for using vibrate in the AndroidManifest?: <uses-permission android:name="android.permission.VIBRATE" />GeorgeLBA
yes this is not my problem when i click buton to test its work but in background not workingMed Elgarnaoui
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 helpsGeorgeLBA
yes i tried that but the same problemMed Elgarnaoui
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

3 Answers

3
votes

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"
}
2
votes

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.
1
votes

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.