2014-06-21 109 views
0

我創建一個服務推送通知:動作按鈕不起作用的Android

AppointmentService.java

Intent intent = new Intent(AppointmentService.this, ReAppointmentService.class); 

intent.putExtra("id", id); 

PendingIntent pIntent = PendingIntent.getService(AppointmentService.this, 0, intent, 0); 

Notification n = new NotificationCompat.Builder(AppointmentService.this) 
    .setContentTitle((String) snapshot.child("name").getValue()) 
    .setContentText("Hey!") 
    .setSmallIcon(R.drawable.ic_launcher) 
    .setAutoCancel(true) 
    .addAction(R.drawable.ic_action_reply_light, "Hey yo!", pIntent) 
    .build(); 

NotificationManager notificationManager = 
     (NotificationManager) HeyService.this.getSystemService(Activity.NOTIFICATION_SERVICE); 

notificationManager.notify(getNotificationID(id), n); 

ReAppointmentService.java @覆蓋 公衆的IBinder onBind (意圖arg0) { return null; }

@Override 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 
    Bundle extras = intent.getExtras(); 

    String id = extras.getString("id"); 

    gc = new GlobalContents(this); 

    Firebase send = new Firebase("https://appointmentapp.firebaseio.com/app/"+id); 

    Firebase ref = send.push(); 

    ref.runTransaction(new Transaction.Handler() 
    { 
      @Override 
      public Transaction.Result doTransaction(MutableData ref) 
      { 
       ref.child("date").setValue(String.valueOf((new GregorianCalendar()).getTimeInMillis())); 
       ref.child("name").setValue(gc.getName()); 
       ref.child("from").setValue(gc.getID()); 
       ref.child("seen").setValue(false); 

       return Transaction.success(ref); 
      } 

      @Override 
      public void onComplete(FirebaseError error, boolean committed, DataSnapshot currentData) { 

      } 
    }); 

    return START_NOT_STICKY; 
} 

該通知通常用操作按鈕顯示。但是,當我點擊按鈕時,什麼都沒有發生(我嘗試在ReAppointmentService上放置一些日誌,但它不起作用)。

什麼問題?

回答

0

PendingIntent.getService的最後一個參數應該幾乎總是FLAG_UPDATE_CURRENT - 否則你的PendingIntent不會有新的演員按上的PendingIntent的文檔進行更新:

一個的PendingIntent本身很簡單,就是維護一個令牌的引用該系統描述了用於檢索它的原始數據。這意味着,即使其擁有的應用程序的進程被終止,PendingIntent本身仍然可以從其他被賦予的進程中使用。如果稍後創建的應用程序重新獲取相同類型的PendingIntent(相同的操作,相同的Intent操作,數據,類別和組件,以及相同的標誌),它將會收到一個表示相同標記的PendingIntent(如果該標記仍然有效)因此調用cancel()將其刪除。

由於這種行爲,重要的是要知道何時兩個Intents被認爲是相同的,用於檢索PendingIntent。人們犯的一個常見錯誤是創建多個PenttentIntent對象,其Intents只在其「額外」內容中有所不同,期望每次都得到不同的PendingIntent。這不會發生。用於匹配的Intent部分與Intent.filterEquals定義的部分相同。如果您使用兩個與Intent.filterEquals等效的Intent對象,那麼您將爲它們獲得相同的PendingIntent。

+0

我已經理解了這個問題,但是這不起作用。接收PendingIntent的服務可能有問題嗎? –

+0

你必須在你的帖子中解釋更多關於'ReAppointmentService'中的日誌指出的內容 - extras.hasExtra(「id」)'是'false'? 'extras.getString(「id」)'爲空?你正在將Intent放入Intent中的ID實際上是一個String嗎? – ianhanniballake

+0

ReAppointmentService似乎沒有被訪問。 –