简体   繁体   中英

No notification sound and vibrate and lights when sending notification from firebase in android background

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());
}

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM