【发布时间】:2017-01-14 01:29:24
【问题描述】:
我正在从 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());
}
【问题讨论】:
-
你在AndroidManifest中定义了使用振动的android权限吗?:
<uses-permission android:name="android.permission.VIBRATE" /> -
是的,这不是我的问题,当我单击按钮以测试其工作但在后台不工作时
-
您是否尝试过使用 NotificationCompat 中的
setDefaults(DEFAULT_ALL)来检查它是否适用于用户的默认首选项?并移除您为测试而定义的振动、灯光和声音。希望这会有所帮助 -
是的,我试过了,但同样的问题
-
我有一个带有振动通知的应用程序。与您的代码的唯一区别是我有这样的代码:
new NotificationCompat.Builder(this).setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).setVibrate(new long[]{400,500,300});not defined from themBuilder.
标签: android firebase firebase-cloud-messaging