当您以Android 8.0(API级别26)为目标时,必须实现一个或多个通知通道才能向用户显示通知。
int NOTIFICATION_ID = 234;
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String CHANNEL_ID = "my_channel_01";
CharSequence name = "my_channel";
String Description = "This is my channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message);
Intent resultIntent = new Intent(ctx, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
notificationManager.notify(NOTIFICATION_ID, builder.build());
要注意的一件有趣的事情:如果已经存在一个通道,则调用createNotificationChannel
无效,因此可以create all channels whenever you start your application。
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int)(System.currentTimeMillis()/1000), mBuilder.build());
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String id = "my_channel_01";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(id, name,importance);
mChannel.enableLights(true);
mNotificationManager.createNotificationChannel(mChannel);
第一种方法是在构造函数中设置通知通道:
Notification notification = new Notification.Builder(MainActivity.this , id).setContentTitle("Title"); mNotificationManager.notify("your_notification_id", notification);
第二种方法是通过Notificiation.Builder.setChannelId()设置通道
Notification notification = new Notification.Builder(MainActivity.this).setContentTitle("Title"). setChannelId(id); mNotificationManager.notify("your_notification_id", notification);
希望这会有所帮助
public static final String NOTIFICATION_CHANNEL_ID = "4655";
//Notification Channel
CharSequence channelName = NOTIFICATION_CHANNEL_NAME;
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
然后在构造函数中使用频道ID:
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID) .setDefaults(Notification.DEFAULT_ALL) .setSmallIcon(R.drawable.ic_timers) .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}) .setSound(null) .setContent(contentView) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setLargeIcon(picture) .setTicker(sTimer) .setContentIntent(timerListIntent) .setAutoCancel(false);
将您的依赖关系更新为com.google.firebase:firebase-messaging:15.0.2
或更高。
这将纠正错误
failed to post notification on channel null
当您的应用在后台接收通知时,因为现在Firebase提供了具有基本设置的默认通知通道。但是您也可以在清单中为FCM指定默认的通知通道。
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id"/>
要了解更多here
public static final int PRIMARY_FOREGROUND_NOTIF_SERVICE_ID = 1001;
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String id = "_channel_01";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(id, "notification", importance);
mChannel.enableLights(true);
Notification notification = new Notification.Builder(getApplicationContext(), id)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My chat")
.setContentText("Listening for incoming messages")
.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannel(mChannel);
mNotificationManager.notify(PRIMARY_FOREGROUND_NOTIF_SERVICE_ID, notification);
}
startForeground(PRIMARY_FOREGROUND_NOTIF_SERVICE_ID, notification);
}
}
这不是最好的解决方案,而只是在后台启动服务确实起作用
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
builder = new NotificationCompat.Builder(this, id);
通知有必填项:
builder.setContentTitle() // required
.setSmallIcon() // required
.setContentText() // required
请参见On Android 8.1 API 27 notification does not display中的示例。
String CHANNEL_ID = "my_channel";
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,CHANNEL_ID);