首页 > 代码库 > 发送通知

发送通知

关键代码:

 NotificationManager manager = (NotificationManager)
                    getSystemService(NOTIFICATION_SERVICE);

        // 单击通知,后跳转
            PendingIntent intent = PendingIntent.getActivity(this,
                    1,
                    new Intent(this, MainActivity.class),
                    0);


            Notification notification = new Notification.Builder(this).
                    setSmallIcon(R.mipmap.ic_launcher).
                    setContentTitle("通知").
                    setContentText("我是一个通知").
                    setContentIntent(intent).
                    setDefaults(Notification.DEFAULT_ALL). // 设置用手机默认的震动或声音来提示
                    build();
            manager.notify(1, notification); //1为通知编号

 

单击通知跳转到对应活动页面,取消通知代码:

NotificationManager manager = (NotificationManager)
                getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(1); //1为之前的通知编号

 

发送通知