您好,登錄后才能下訂單哦!
正文: |
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent == null || context == null) { return; } mNotificationManager.cancel(NOTIFICATION_ID_LIVE); String type = intent.getStringExtra(PUSH_TYPE); if (PUSH_TYPE_LINK.equals(type)) { //mNumLinkes = 0; } else if (PUSH_TYPE_LIVE.equals(type)) { //mNumLives = 0; } //這里可以重新計數 } }; private void sendLiveNotification() { Intent intent = new Intent(NOTIFICATION_CLICK_ACTION); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); String title = "Push測試"; mBuilder.setContentTitle(title); mBuilder.setTicker(title); mBuilder.setContentText("https://233.tv/over140"); mBuilder.setLargeIcon(BitMapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); mBuilder.setSmallIcon(R.drawable.ic_action_cast); mBuilder.setDefaults(Notification.DEFAULT_ALL); mBuilder.setWhen(System.currentTimeMillis()); mBuilder.setContentIntent(PendingIntent.getBroadcast(this, NOTIFICATION_ID_LIVE, intent, 0)); mBuilder.setDeleteIntent(PendingIntent.getBroadcast(this, NOTIFICATION_ID_LIVE, new Intent(NOTIFICATION_DELETED_ACTION).putExtra(PUSH_TYPE, PUSH_TYPE_LIVE), 0)); mNotificationManager.notify(NOTIFICATION_ID_LIVE, mBuilder.build()); }
代碼說明
1、最重要的是setDeleteIntent,這個在API Level 11(Android 3.0) 新增的
2、注意不要設置setAutoCancel為true,否則監聽器接收不到
3、這里統一了點擊通知和消除通知的操作
4、注意廣播在推送前要注冊好
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。