推送通知未显示在通知栏上 [英] Push notification is not showing on notification bar
问题描述
我已经通过关注 YouTube 视频实现了 firebase 推送通知,并且当应用程序在后台运行时它可以正常工作,但是当应用程序运行在前台时,它不会显示通知,但我可以在 logcat 中看到通知.
I have implemented the firebase push notification by following a YouTube video and its working fine when app is in the background but when the app goes on the foreground it will not show the notification but I can see the notification coming in the logcat.
这是代码-
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
getFirebaseMessage(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
public void getFirebaseMessage(String title, String msg){
Log.d("abc", title);
Log.d("abc", msg);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "plasmatechannel")
.setSmallIcon(R.drawable.ic_baseline_notifications_active_24)
.setContentTitle(title)
.setContentText(msg)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
managerCompat.notify(101 , builder.build());
}
}
推荐答案
从 API 级别 26 开始,所有通知都必须分配给一个频道,以设置应用于该频道中所有通知的行为.
Starting from API level 26, all notifications must be assigned to a channel to set the behavior that is applied to all notifications in that channel.
请尝试在现有代码中实现以下方式
Please try to implement the below way in your existing code
NotificationCompat.Builder notificationBuilder = null;
try {
if (Build.VERSION.SDK_INT >= 26) {
try{
NotificationManager notificationManager = (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.deleteNotificationChannel(Constant.CHANNEL_ID);
NotificationChannel channelnews = new NotificationChannel(Constant.CHANNEL_ID, "Breaking News", NotificationManager.IMPORTANCE_HIGH);
channelnews.enableLights(true);
channelnews.setShowBadge(false);
channelnews.enableVibration(true);
channelnews.setLightColor(Color.WHITE);
channelnews.setVibrationPattern(new long[]{100, 200, 100, 200, 100, 200, 100});
channelnews.setSound(url,new AudioAttributes.Builder().build());
notificationBuilder = new NotificationCompat.Builder(this, Constant.CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher);
notificationManager.createNotificationChannel(channelnews);
}catch (Exception e){
e.printStackTrace();
}
} else {
notificationBuilder = new NotificationCompat.Builder(ctx,"")
.setSmallIcon(R.mipmap.ic_launcher);
}
} catch (Exception e) {
e.printStackTrace();
}
if (notificationBuilder == null) {
notificationBuilder = new NotificationCompat.Builder(ctx,"")
.setSmallIcon(R.mipmap.ic_launcher);
}
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(message);
notificationBuilder.setSubText(subtext);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationNumber, notificationBuilder.build());
详情请阅读https://developer.android.com/training/notify-user/channels
https://developer.android.com/reference/android/app/NotificationChannel
希望能帮到你
这篇关于推送通知未显示在通知栏上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!