我有一个应该显示推送通知的应用,但它没有
此行new NotificationCompat.Builder(this);
在代码中被取消。
我是新手,这怎么可行
这是FirebaseMessagingService.java类中的代码
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
sendNotification(remoteMessage.getNotification().getBody());
}
private void sendNotification(String MessageBody){
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 defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(R.drawable.ic_launcher_background);
notificationBuilder.setContentTitle("Hope");
notificationBuilder.setContentText(MessageBody);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(defaultSoundUri);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
}
}
答案 0 :(得分:1)
尝试使用:
NotificationChannel mChannel = new NotificationChannel("", "", 1);
notificationManager.createNotificationChannel(mChannel);
答案 1 :(得分:1)
这似乎是here的重复问题。
由于 Android O (表示您的targetSdk为26或更高),如果没有notification channel,通知就不会显示。
这是我的工作代码:)
Context c = getApplicationContext();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// The id of the channel.
final String CHANNEL_ID = "default";
// The user-visible name of the channel.
final String CHANNEL_NAME = "Default";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel defaultChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(defaultChannel);
}
intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(c, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(c, CHANNEL_ID)
.setLargeIcon(your_custom_bitmap)
.setSmallIcon(R.drawable.ic_notification)
.setColor(ContextCompat.getColor(c, R.color.notification))
.setContentTitle(getString(R.string.app_name))
.setContentText(message)
.setWhen(System.currentTimeMillis())
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setLights(ContextCompat.getColor(c, R.color.colorAccentLight), 5000, 5000)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent);
notificationManager.notify("myapp", 0, notificationBuilder.build());
答案 2 :(得分:0)
如果您从Firebase控制台推送通知,则只有在应用程序处于后台或未运行时才会显示通知。您可以在HERE了解更多信息。如果要接收onMessageReceived()方法的通知数据,则必须从后端发送通知