0
我是Android GCM和通知事物的新手。現在,我被困在如何更新源碼時,用戶選項卡通知按鈕PendingIntent更新sqlite
的情況是
- 我的服務器將發送GCM向GCM服務器
- GCM服務器發送消息到設備
- 一旦IntentService收到消息,在本地sqlite中創建新的消息記錄。
- 2按鈕,創建在通知欄新通知認爲,「啓動應用」,「標記爲已讀」和「辭退」
的「啓動程序」按鈕很簡單,每一個教程將展示如何啓動通過使用PendingIntent的新活動。但對於「馬克閱讀」和「解僱」我不知道如何實現它(而英語不是我的母語,我不能拿出關鍵字來尋找答案....所以,對不起,如果這個問題已經被問)
- 對於「標記爲已讀」按鈕,我希望它調用一個服務或應用程序子類來更新源碼,然後取消通知,而無需啓動一個活動
- 對於「辭退」按鈕,我希望它僅僅取消通知
這是有
代碼@Override
protected void onHandleIntent(Intent intent)
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("Title of the notification");
builder.setSmallIcon(R.drawable.notification_icon);
builder.setStyle(new Notification.BigTextStyle().bigText("The message that I want to show"));
builder.setAutoCancel(true);
Intent i1 = new Intent(this, MainActivity.class);
PendingIntent pi1 = PendingIntent.getActivity(this, UNIQUE_ID, i1, PendingIntent.FLAG_ONE_SHOT);
PendingIntent pi2 = null; // To mark as read, I don't how to write it yet.
PendingIntent pi3 = null; // To dismiss, I don't how to write it yet.
builder.addActon(R.drawable.launch_app_icon, "Launch app", pi1);
builder.addActon(R.drawable.mark_as_read_icon, "Mark as read", pi2);
builder.addActon(R.drawable.dismiss_icon, "Dismiss", pi3);
Notification notification;
if (DeviceHelper.getAndroidSDK() < 16) {
notification = builder.getNotification();
} else {
notification = builder.build();
}
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(UNIQUE_ID, notification);
}
我也試圖設置PI2是這樣
Intent i2 = new Intent(MyApp.USER_DISMISS_NOTIFICATION);
PendingIntent pi2 = PendingIntent.getBroadcast(this, UNIQUE_ID, i2, PendingIntent.FLAG_ON_SHOT);
再聽聽本地廣播在Application子類
public final class MyApp extends Application {
public static final USER_DISMISS_NOTIFICATION = "UserDismissNotification";
public void onCreate() {
super.onCreate();
LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Local broadcast received, update database
}
}, new IntentFilter(USER_DISMISS_NOTIFICATION));
}
}
但廣播接收器的的onReceive永遠不會被調用。
任何建議,歡迎,謝謝!
如果你想發送意圖廣播接收器,你應該使用PendingIntent.getBroadcast方法,而不是PendingIntent.getActivity – 2015-10-12 15:27:04