我尝试使用Firebase控制台向手机发送推送通知,但是当应用程序最小化时,通知的声音和标题不起作用,并且当应用程序位于前台工作正常,行为通知不同的原因是什么?
MyFirebaseMessagingService.java
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, DashboardActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_info_black_24dp)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
的AndroidManifest.xml
<!-- [START firebase_service] -->
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<!-- [END firebase_service] -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
答案 0 :(得分:1)
我认为您正在使用通知消息类型,该类型仅在应用程序位于前台时显示通知。当应用程序在后台时,您需要使用数据消息类型来获取通知(以及声音,振动等)。检查我的回答here。