我已经正确地实现了Fire基础通知,一些设备没有接收到内部通知,在运行后台时会聚焦如何解决该问题
public void getNotification(String icon,String title,String body){
remoteViews = new RemoteViews(getPackageName(),R.layout.custom_notification_big);
remoteView = new RemoteViews(getPackageName(),R.layout.custom_notification_small);
//Bitmap bitmap = SetImage(remote.getIcon()) ;
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1410, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if(icon!=null && !icon.isEmpty()){
remoteViews.setImageViewBitmap(R.id.image_pic,bitmap);
}else{
}
if(title != null && !title.isEmpty()){
remoteViews.setTextViewText(R.id.title,title);
}
if(body != null && !body.isEmpty()){
remoteViews.setTextViewText(R.id.text,body);
}
NotificationCompat.Builder notificationBuilder1 = new NotificationCompat.Builder(getApplicationContext())
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(remoteView)
.setCustomBigContentView(remoteViews)
.setContentTitle("Notification")
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_VIBRATE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
notificationBuilder1.setSmallIcon(R.mipmap.ic_launcher);
} else
{
notificationBuilder1.setSmallIcon(R.mipmap.ic_launcher);
}
notificationManager.notify(100, notificationBuilder1.build());
}
您需要为此添加频道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.createNotificationChannel(
NotificationChannel(
"YOUR_ID",
"YOUR_NAME",
NotificationManager.IMPORTANCE_HIGH
)
)
}
没有收到通知!
在以下情况下使firebase调用onMessageReceived()
1)应用程序在前台2)应用程序在后台3)应用程序已被杀死
您不能将JSON密钥“通知”放入您的请求/ InApp到firebase API,而是使用“数据”。
你可以按照文档。 https://firebase.google.com/docs/cloud-messaging/concept-options#notifications