通知不显示Firebase Android 8 [英] Notification does not show firebase Android 8
本文介绍了通知不显示Firebase Android 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个应该显示推送通知的应用程序,但没有这行 new NotificationCompat.Builder(this);
在代码中被取消了.我是新来的,这怎么工作这是FirebaseMessagingService.java类中的代码
I have an app that is supposed to show a push notification but it doesn't
this line new NotificationCompat.Builder(this);
is canceled out in the code.
I'm new to this, how can this work
here is the code in FirebaseMessagingService.java class
@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());
}
}
推荐答案
这里.
由于 Android O (这意味着您的targetSdk为26或更高版本),因此没有
Since Android O (It means your targetSdk is 26 or above), notification will not be showing without 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());
这篇关于通知不显示Firebase Android 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文