Android工具集

Android消息通知Notification学习笔记

2019-07-05  本文已影响1人  Small_Cake
1.创建通知,并显示
    /**
     *  简易通知显示
     *  必要三元素:smallIcon,contentTitle,contentText
     */
    public static void showNotice(Context context, CharSequence msg){
            NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
            NotificationCompat.Builder builder;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel("渠道ID", "优惠券商品", NotificationManager.IMPORTANCE_LOW);
                manager.createNotificationChannel(channel);
                builder = new NotificationCompat.Builder(context, "渠道ID");
            }else {
                builder = new NotificationCompat.Builder(context);
            }
            Notification notification = builder
                    .setSmallIcon(android.R.drawable.sym_def_app_icon)
                    .setContentTitle(context.getString(R.string.app_name))
                    .setContentText(msg)
                    .build();
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            manager.notify(0,notification);
    }
2.点击通知,跳转到主页面
private void showNotice(Context context,String msg){
        NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("渠道ID", "优惠券商品", NotificationManager.IMPORTANCE_LOW);
            manager.createNotificationChannel(channel);
            builder = new NotificationCompat.Builder(context, "渠道ID");
        }else {
            builder = new NotificationCompat.Builder(context);
        }
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0,  intent, PendingIntent.FLAG_CANCEL_CURRENT);
        Notification notification = builder
                .setSmallIcon(R.mipmap.logo)
                .setContentTitle("我是一个标题")
                .setContentText(msg)
                .setContentIntent(contentIntent)
                .build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        manager.notify(0,notification);
    }
3.点击通知,打开多个页面
 private void showNotice(Context context,String msg){
        NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("渠道ID", "优惠券商品", NotificationManager.IMPORTANCE_LOW);
            manager.createNotificationChannel(channel);
            builder = new NotificationCompat.Builder(context, "渠道ID");
        }else {
            builder = new NotificationCompat.Builder(context);
        }
        PendingIntent contentIntent = PendingIntent.getActivities(getApplicationContext(), 0,  makeIntentStack(context), PendingIntent.FLAG_CANCEL_CURRENT);
        Notification notification = builder
                .setSmallIcon(R.mipmap.logo)
                .setContentTitle("我是一个标题")
                .setContentText(msg)
                .setContentIntent(contentIntent)
                .build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        manager.notify(0,notification);
    }
    Intent[] makeIntentStack(Context context) {
        Intent[] intents = new Intent[2];
        intents[0] = Intent.makeRestartActivityTask(new ComponentName(context, MainActivity.class));
        intents[1] = new Intent(context, WhiteBarActivity.class);
        return intents;
    }

参考:Notification点击跳转指定的Activity

其实也可以用AndroidStudio创建一个Notification,只需修改部分参数即可

上一篇下一篇

猜你喜欢

热点阅读