通知Notification与PendingIntent

-- Notification与PendingIntent ;Notification通过发广播,然后根据不同的Action跳转不同的页面

在Service接收广播后,发送通知到通知栏;在BroadcastReceiver接收广播后,发送通知到通知栏;

Notification在SDK8.0有比较大的改动。

public class PendingNotification {
    public void sendNotif(Context context) {
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setContentTitle("测试标题")// 设置通知栏标题
                .setContentText("测试内容")// /<span style="font-family:
                // Arial;">/设置通知栏显示内容</span>
                .setContentIntent(getDefalutIntent(context, Notification.FLAG_AUTO_CANCEL)) // 设置通知栏点击意图
                // .setNumber(number) //设置通知集合的数量
                .setTicker("测试通知来啦") // 通知首次出现在通知栏,带上升动画效果的
                .setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
                .setPriority(Notification.PRIORITY_DEFAULT) // 设置该通知优先级
                .setAutoCancel(true)// 设置这个标志当用户单击面板就可以让通知将自动取消
                .setOngoing(true)// ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或
                //以某种方式正在等待, 因此占用设备(如一个文件下载, 同步操作, 主动网络连接)
                .setDefaults(Notification.DEFAULT_VIBRATE)// 向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用
                //户默认设置,使用defaults属性,可以组合
                // Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 添加声音 //
                // requires VIBRATE permission
                .setSmallIcon(R.mipmap.ic_launcher);// 设置通知小ICON
        // mNotificationManager.notify();
        // Notification notify = mBuilder.build();
        // notify.flags = Notification.FLAG_AUTO_CANCEL;
        mNotificationManager.notify(11, mBuilder.build());
    }

    public PendingIntent getDefalutIntent(Context context, int flags) {
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, flags);
        return pendingIntent;
    }
}

//android 广播接收器之通知栏页面跳转

public class MyBroadcastReceiver extends BroadcastReceiver {
    private static int NOTIFY_ID = 1000;
    private static final String tag = "NotificationReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Boot Complete", Toast.LENGTH_SHORT).show();
        //推送消息
        sendNotification(context, "12345");
    }

    private void sendNotification(Context context, String mas) {
        //【1】获取Notification 管理器的参考
        NotificationManager notifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        //【2】设置通知。PendingIntent表示延后触发,是在用户下来状态栏并点击通知时触发,触发时PendingIntent发送intent,打开到指定页面。
        Intent intent = new Intent(context, MainActivity.class);
        // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // context.startActivity(intent );
        // intent.setClassName(this,MainActivity.class);
        //intent.setComponent(new ComponentName(this, MainActivity.class));  //方法4
        // 主要设置点击通知的时显示内容的类
        PendingIntent pi = PendingIntent.getActivity(context, 0, intent, 0);
        Notification notification = new Notification.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setTicker("Hello")
                .setContentTitle("Title")
                .setContentText("Content text")
                .setContentIntent(pi)
                .build();
        //点击后删除,如果是FLAG_NO_CLEAR则不删除,FLAG_ONGOING_EVENT用于某事正在进行,例如电话,具体查看参考。
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        //【3】发送通知到通知管理器。第一个参数是这个通知的唯一标识,通过这个id可以在以后cancel通知,
        // 更新通知(发送一个具有相同id的新通知)。这个id在应用中应该是唯一的。
        notifyMgr.notify(NOTIFY_ID, notification);
    }
}


猜你喜欢

转载自blog.csdn.net/shareus/article/details/80707404