【发布时间】:2019-07-11 07:45:19
【问题描述】:
我在我的应用中集成了 FCM。从 Firebase 发送了 fcm,但没有显示在设备上。我正在尝试在我的应用前景和背景时显示通知。
1-:将应用程序连接到 Firebase 并将 FCM 添加到我的应用程序。(Android Studio-->Tools-->Firebase-->Cloud Messaging)
2-:创建MyFirebaseMessageingService1类并通过FirebaseMessagingService扩展并实现方法(onNewToken和onMessageReceived)
3-:在onMessageReceived中创建方法genrateNotification
4-:在清单文件中添加此服务并添加操作 com.google.firebase.MESSAGING_EVENT
private void genrateNotification(String body, String title) {
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
if(NOTIFICATION_ID > 1073741824){
NOTIFICATION_ID =0;
}
notificationManager.notify(NOTIFICATION_ID++,notificationBuilder.build());
}
执行上述代码后,无法获取令牌和通知
【问题讨论】:
-
通过登录 onMessageReceived() 方法检查。首先检查您的设置。将通知优先级设为高,然后检查清单中的 INTERNET Premission。
标签: android firebase push-notification firebase-cloud-messaging