我正在从firebase向我的Android应用程序发送推送通知 . 但是当我的应用程序处于后台时,不会调用firebase onMessageReceived方法而是调用firebase向系统发送通知,以便在系统托盘中显示通知 . 通知出现在系统托盘中,但没有通知声音,即使我已在系统设置中允许我的应用程序发出通知声音 . 我的通知代码谢谢你
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());
}
2 回答
从Firebase控制台转到通知>新消息>高级选项,并将通知作为自定义数据发送 . 使用'title'和'body'作为Key并将其值设置为您希望在通知中显示它们 . 当你的应用程序处于后台或被杀时,这应该调用onMessageReceived() .
如果您要从自己的服务器发送通知,这里有一个示例json部分:
它写在override sample of onMessageReceived()中,第二条评论说:
看评论链接
解决方案,如相关问题的答案:How to handle notification when app in background in Firebase,可以在Messages with both notification and data payloads部分的文档中找到,其中说: