2014-11-01 29 views
3

我有一個推送通知應用程序。 當推送通知到來時,廣播接收器調用GCMIntentService設置通知Android:從NotificationManager調用IntentService action PendingIntent

mNotificationManager = (NotificationManager) this 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     Intent intent = new Intent(this, HomeActivity.class); 
     Intent intent = new Intent(this, AddToCalendarIntentService.class); 
     intent.putExtra(JSON_KEY_CLASS_ID, classid); 
     intent.putExtra(JSON_KEY_CLASS_NAME, classname); 
     intent.putExtra(Config.JSON_KEY_DATE, tgl); 
     intent.putExtra(Config.JSON_KEY_TIME_START, timestart); 
     intent.putExtra(Config.JSON_KEY_TIME_END, timeend); 
     intent.putExtra(Config.JSON_KEY_VENUE, venue); 
     Log.d(LOG_TAG, classid + ", " + classname + ", " + venue); 
     PendingIntent contentIntent = PendingIntent.getService(this, 0, 
       intent, 0); 

     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(this) 
         .setSmallIcon(R.drawable.ic_notif) 
         .setContentTitle("Re: " + classname) 
         .setStyle(new NotificationCompat.BigTextStyle() 
           .bigText(pesan)) 
         .setContentText(pesan) 
         .setDefaults(NotificationCompat.DEFAULT_VIBRATE) 
         .setDefaults(NotificationCompat.DEFAULT_LIGHTS) 
         .setDefaults(NotificationCompat.DEFAULT_SOUND) 
         .addAction(R.drawable.ic_action_add_person, "Add to Calendar", contentIntent) 
         .setAutoCancel(true) 
         .setTicker("Reschedule Class"); 

     mBuilder.setContentIntent(contentIntent); 
     mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 

當添加用戶點擊日曆,它調用的PendingIntent與所有參數啓動AddToCalendarIntentService。

public class AddToCalendarIntentService extends IntentService { 

public AddToCalendarIntentService() { 
    super("AddToCalendarIntentService"); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    final String JSON_KEY_CLASS_NAME = "classname"; 
    final String JSON_KEY_CLASS_ID = "classid"; 
    Bundle extra = intent.getExtras(); 
    String title = extra.getString(JSON_KEY_CLASS_NAME); 
    String location = extra.getString(Config.JSON_KEY_VENUE); 
    String tgl = extra.getString(Config.JSON_KEY_DATE); 
    String[] tglSplit = tgl.split("-"); 
    int year = Integer.parseInt(tglSplit[0]); 
    int month = Integer.parseInt(tglSplit[1]); 
    int date = Integer.parseInt(tglSplit[2]); 

    String timestart = extra.getString(Config.JSON_KEY_TIME_START); 
    String timeend = extra.getString(Config.JSON_KEY_TIME_END); 
    String[] start = timestart.split(":"); 
    String[] end = timeend.split(":"); 
    int hourStart = Integer.parseInt(start[0]); 
    int minStart = Integer.parseInt(start[1]); 
    int hourEnd = Integer.parseInt(end[0]); 
    int minEnd = Integer.parseInt(end[1]); 
    Log.d("INTENT SERVICE", location); 
    TaskHelper.addToCalendar(this, "Reschedule: " + title, location, year, month-1, date, hourStart, minStart, hourEnd, minEnd); 
    NotificationManager mNotification = (NotificationManager) this 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotification.cancel(1); 
} 
} 

代碼工作,但如果我再次得到了其他通知,並將其添加到日曆,在AddToCalendarIntentService參數保留舊的參數,忽視了新的。 例如,在第一個位置是印尼,既GCMIntentServiceAddToCalendarIntentService日誌顯示印尼

在第二個通知,該位置是新加坡,它正確地顯示在GCMIntentService,但在AddToCalendarIntentService登錄時,它顯示印尼(這應該是新加坡)。

請幫忙。

謝謝。

+0

瞭解的getService() – pskink 2014-11-01 12:17:53

+0

第二PARAM的文檔謝謝@pskink – 2014-12-25 10:01:16

回答