美文网首页
android notification的使用

android notification的使用

作者: 清汤白面 | 来源:发表于2018-10-15 13:30 被阅读0次
public static void createNotification(Context context, String title, String content, PendingIntent pendingIntent) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        // 通知渠道的id
        String id = "channel";
        // 用户可以看到的通知渠道的名字.
        CharSequence name = "消息渠道";
        // 用户可以看到的通知渠道的描述
        String description = "通知用户消息";
        NotificationCompat.Builder notificationBuilder;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = new NotificationChannel(id, name, importance);
            //  配置通知渠道的属性
            mChannel.setDescription(description);
            // 设置通知出现时的闪灯(如果 android 设备支持的话)
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.RED);
            // 设置通知出现时的震动(如果 android 设备支持的话)
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            notificationManager.createNotificationChannel(mChannel);
            notificationBuilder = new NotificationCompat.Builder(context, id);
        } else {
            notificationBuilder = new NotificationCompat.Builder(context);
        }
        notificationBuilder.setSmallIcon(R.mipmap.ic_logo)
                .setContentTitle(title)
                .setContentText(content)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);
        Random random = new Random();
        notificationManager.notify(random.nextInt(), notificationBuilder.build());
    }
// 下面是
使用

            Intent intent1;
            LogUtil.v("======URL=====" + url);
            if (!TextUtils.isEmpty(url)) {
                intent1 = new Intent(context, BaseAgentActivity.class);          
                intent1.putExtra("url", url);
                intent1.putExtras(bundle1);
            } else {
                String appPackageName = AppUtils.getAppPackageName();
                intent1 = context.getPackageManager().
                        getLaunchIntentForPackage(appPackageName);
                intent1.setFlags(
                        Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            }
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent1
                    , PendingIntent.FLAG_ONE_SHOT);
            createNotification(context, title, content, pendingIntent);

相关文章

网友评论

      本文标题:android notification的使用

      本文链接:https://www.haomeiwen.com/subject/cvrfzftx.html