2016-04-12 84 views
1

我有一個通知系統工作,服務是scheculing然後通知工作正常。通過服務的Android通知

我有一些需要用戶輸入/操作的通知。

這裏就是我建立我的通知代碼:

public int onStartCommand(Intent intent, int flag, int startId) 
{ 
    super.onStartCommand(intent , flag, startId); 

    Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    Context context = this.getApplicationContext(); 
    notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE); 
    Intent mIntent = new Intent(this, MainActivity.class); 
    pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    Notification builder = null; 
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {   
     builder = new Notification.Builder(this) 
       .setContentTitle(Utils.Title) 
       .setContentText(Utils.Body) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .addAction(0, Utils.button1Value, pendingIntent) 
       .addAction(0, Utils.button2Value, pendingIntent) 
       .setSound(uri) 
       .setContentIntent(pendingIntent) 
       .build(); 

    } 

    notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
    notificationManager.notify(Utils.NotificationID, builder);  

    return START_STICKY; 

我的問題是:我如何獲得點擊哪個按鈕通知用戶?換句話說,如何爲通知中的這兩個操作創建某種按鈕偵聽器?

此外,如何堆疊我的通知,使他們不會出現所有分開?我在Google API上閱讀過我需要使用.setGroup,但這不起作用。 任何人都可以分享一些通知堆疊的例子嗎?

**********增加*************

final static String GROUP_KEY = "myGroup"; // added String to identify the group 

public int onStartCommand(Intent intent, int flag, int startId) 
{ 
super.onStartCommand(intent , flag, startId); 

Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

Context context = this.getApplicationContext(); 
notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE); 
Intent mIntent = new Intent(this, MainActivity.class); 
pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

Notification builder = null; 
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {   
    builder = new Notification.Builder(this) 
      .setContentTitle(Utils.Title) 
      .setContentText(Utils.Body) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setGroup(GROUP_KEY) // added group for Stacking 
      .addAction(0, Utils.button1Value, pendingIntent) 
      .addAction(0, Utils.button2Value, pendingIntent) 
      .setSound(uri) 
      .setContentIntent(pendingIntent) 
      .build(); 

} 

notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
notificationManager.notify(0, builder); // ID is always the same because the notifications are all stacking into one, tried with unique ID not working either 

return START_STICKY; 

回答

2
  1. 對於按鈕偵聽問題:

通知行動將觸發該pendingIntent,所以只需將您的自定義意圖放入它。每個文本都應該有不同的pendingIntent,以便您可以區分它。 。

public int onStartCommand(Intent intent, int flag, int startId) { 
    //...... 
    Intent mIntent1 = new Intent(this, MainActivity.class); 
    mIntent1.setAction("clickAc1"); 
    PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0, mIntent1, PendingIntent.FLAG_UPDATE_CURRENT); 

    Intent mIntent2 = new Intent(this, MainActivity.class); 
    mIntent2.setAction("clickAc2"); 
    PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, mIntent2, PendingIntent.FLAG_UPDATE_CURRENT); 

    Intent it = new Intent(this, MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, it, PendingIntent.FLAG_UPDATE_CURRENT); 

    Notification builder = null; 
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { 
     builder = new Notification.Builder(this) 
       .setContentTitle("Title") 
       .setContentText("Text") 
       .setSmallIcon(R.drawable.your_logo) 
       .addAction(0, "ac1", pendingIntent1) 
       .addAction(0, "ac2", pendingIntent2) 
       .setSound(uri) 
       .setContentIntent(pendingIntent) 
       .build(); 

    } 

,當用戶單擊該文本ac1,那麼您可以在MainActivity(或您希望其他方面處理它

//MainActivity 
@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    Log.d("probe","onNewIntent:"+intent.getAction()); 
    //response your click event by case intent.getAction(), 
    //it will be `clickAc1` or `clickAc2`, the string you write down in intent.setAction 
} 

如果使用自定義RemoteViews,你可以按照如下:

Intent intent = new Intent("Your Custom intent string, like com.yourpackage.ClickButtonXXX"); 
    pendingIntent = PendingIntent.getService(getApplicationContext(), 
     yourRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    mRemoteViews.setOnClickPendingIntent(R.id.yourViewId, pendingIntent); 

然後你可以用覆蓋響應Service.onStartCommand(Intent intent, int flags, int startId), case intent.getAction();來區分不同的按鈕。

  • 堆疊消息:
  • 我發現這個在此notification history article

    .setGroup(GROUP_KEY_MESSAGES) // added group for Stacking 
    .setGroupSummary(true) 
    .setContentTitle(count+" new notes") 
    .setStyle(new NotificationCompat.BigTextStyle().setSummaryText(count+" more")) 
    .build(); 
    
    count++; 
    

    如果使用Notification,更改NotificationCompat.BigTextStyle()Notification.BigTextStyle()

    我認爲這可能是你想要的。

    彙總通知祕籍有點

    +0

    感謝您的答案,我將調查鏈接和您的建議。對於1個問題,我不完全理解如何將這個應用於我的例子。你能把它應用到我的通知代碼嗎? –

    +0

    ummm,我使用自定義通知佈局,所以用這種方式實現。你的'addAction(0,Utils.button1Value,pendingIntent)'不起作用,因爲第一個參數需要圖標資源ID。似乎你需要一個[自定義通知](http://stackoverflow.com/questions/27673943/notification-action-button-not-clickable-in-lock-screen)未來。 – sakiM

    +0

    是的,我爲圖標添加了0,因爲我不需要2個動作的圖標,只是文本,它顯示完美。我已經看到與RemoveViews&自定義佈局的鏈接,可能我會嘗試。但根據我的理解,這是另一種選擇。什麼是setAction選項的「官方」方式?必須有,否則這個選項將不被支持。 2)爲了設置grups我遵循了文檔,它不工作。我將修改後的代碼發佈到setGroup的更新問題中,供您查看。正如我一直在閱讀的那樣,該文檔是針對可穿戴設備的。 –