0

我是Android GCM和通知事物的新手。現在,我被困在如何更新源碼時,用戶選項卡通知按鈕PendingIntent更新sqlite

的情況是

  1. 我的服務器將發送GCM向GCM服務器
  2. GCM服務器發送消息到設備
  3. 一旦IntentService收到消息,在本地sqlite中創建新的消息記錄。
  4. 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永遠不會被調用。

任何建議,歡迎,謝謝!

回答

0

我做它的工作原理如下

A.創建廣播接收器

public class NotificationReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.i("Dev", "Received"); 
    } 
} 

B.註冊接收到Application對象**

<application 
    android:name="com.example.app" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/application_name" 
    android:theme="@android:style/Theme.Holo.NoActionBar" 
    android:hardwareAccelerated="true" > 

    <activity 
     android:name="com.example.app.MainActivity" /> 

    <receiver android:name="com.example.NotificationReceiver" /> 
</application> 

C.創建掛起的意圖如下,並添加它要通知

Intent i = new Intent(this, NotificationReceiver.class); 
PendingIntent pi = PendingIntent.getActivity(this, UNIQUE_ID, i, PendingIntent.FLAG_ONE_SHOT); 

notification.addAction(R.drawable.button_icon, "Do something", pi); 

現在,當用戶選擇一個按鈕時,NotifiationReceiver的onReceive將被調用,然後我可以更新數據庫,啓動活動或基於意向包的參數。

希望這有助於

+0

如果你想發送意圖廣播接收器,你應該使用PendingIntent.getBroadcast方法,而不是PendingIntent.getActivity – 2015-10-12 15:27:04