消息通知Notificatio在8.0上不显示,适配Android8.0

public class NotificationUtil {
    private Context aContext;
    private NotificationManager notificationManager;
    private static class NotificationHolder {
        private static final NotificationUtil INSTANCE = new NotificationUtil();
    }
    private NotificationUtil() {
    }
    public static final NotificationUtil getInstance() {
        return NotificationHolder.INSTANCE;
    }
    /**
     * 初始化变量和适配8.0工作
     *
     * @param context
     */
    @RequiresApi(api = 26)
    public void init(Context context) {
        aContext = context;
        notificationManager = (NotificationManager) aContext.getSystemService(
                NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            String channelId = "chat";
            String channelName = "聊天消息";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotificationChannel(channelId, channelName, importance);
        }
    }
    /**
     * 为8.0 设置通知渠道
     *
     * @param channelId
     * @param channelName
     * @param importance
     */
    @RequiresApi(api = 26)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel notificationchannel = new NotificationChannel(channelId, channelName, importance);
        notificationManager.createNotificationChannel(notificationchannel);
    }
    /**
     * 发送通知消息
     *
     * @param title
     * @param content
     */
    public void sendNotification(String title, String content, Context context, Activity activity) {
        Intent intent = new Intent(context, activity.getClass());
        PendingIntent intentPend = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        Notification notification = new NotificationCompat.Builder(aContext, "chat")
                .setContentTitle(title)
                .setContentText(content)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(aContext.getResources(), R.mipmap.ic_launcher))
                .setAutoCancel(true)
                .setContentIntent(intentPend)
                .build();
        notificationManager.notify(1, notification);
    }
    /**
     * 发送通知消息
     * bundle
     * @param bundle
     * @param context
     */
    public void sendNotificationBundle(Bundle bundle, Context context, Class activity) {
        Intent intent = new Intent(context, activity);
        intent.putExtras(bundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent intentPend = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        String title = bundle.getString(JPushInterface.EXTRA_TITLE);
        String content = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        Notification notification = new NotificationCompat.Builder(aContext, "chat")
                .setContentTitle(title)
                .setContentText(content)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(aContext.getResources(), R.mipmap.ic_launcher))
                .setAutoCancel(true)
                .setContentIntent(intentPend)
                .build();
        notificationManager.notify(1, notification);
    }
}

使用方式:NotificationUtil.init(context).sendNotificationBundle();

或者NotificationUtil.init(context).sendNotification();

Activity参数对应的是点击后要跳转的Activity

上一篇:Arch的使用需知


下一篇:自定义View二篇,如何自定义一个规范的ViewGroup