前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >消息通知Notificatio在8.0上不显示,适配Android8.0

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

作者头像
黄林晴
发布2019-08-31 19:31:25
1.1K0
发布2019-08-31 19:31:25
举报
文章被收录于专栏:代码男人

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/huangliniqng/article/details/83537119

代码语言:javascript
复制
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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年08月30日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档