版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011068702/article/details/82825513
1 问题
app,自定义消息通知的时候,在Android8.0手机上收不到通知
2 解决办法
NotificationManager需要创建NotificationChannel,然后调用createNotificationChannel把NotificationChannel传递进去,并且通过setChannelId设置相应的id
3 普通样本代码实现
private static final String ID = "PUSH_NOTIFY_ID";
private static final String NAME = "PUSH_NOTIFY_NAME";
public int id = 0;
public NotificationManager manager;
public void showMessage() {
manager = (NotificationManager) mContext.getSystemService(mContext.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(ID, NAME, NotificationManager.IMPORTANCE_LOW);
manager.createNotificationChannel(mChannel);
builder.setChannelId(ID);
}
builder.setContentTitle("chenyu");
builder.setContentText("hello word");
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(),R.mipmap.ic_launcher));
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(mContext, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pIntent);
builder.setFullScreenIntent(pIntent,true);
builder.setAutoCancel(true);
Notification notification = builder.build();
manager.notify(id++, notification);
}
但是上面的代码不知道为什么状态栏还没有点击就跳转到了相应的界面,然后点击状态栏的消息同样也会跳,然后采用了下面的普通自定义通知栏
public void showNotifyMsg() {
manager = (NotificationManager) mContext.getSystemService(mContext.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(mContext);
//解决Android8.0收不到消息问题
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(PUSH_CHANNEL_ID, PUSH_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
manager.createNotificationChannel(mChannel);
builder.setChannelId(PUSH_CHANNEL_ID);
}
Intent intent = new Intent(mContext, ReveiveEmailActivity.class);
Bundle bundle = new Bundle();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentTitle("chenyu");
builder.setContentText("hello word");
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher));
builder.setAutoCancel(true);
manager.notify(id++, builder.build());
}