2011-05-24 45 views
1

我正在開發GPS應用程序。如果用戶進入預定義區域內部或外部,我的應用程序將通過NotificationManager觸發通知。我的應用可以針對這兩種情況啓動通知。如何檢測由NotificationManager發送的通知的發件人/檢測通知欄上的哪個通知已被點擊?

從onResume(),我總是得到最新的intent.setExtra()值,而不是intent.setExtra值的點擊通知的值。

的問題是如何檢測對內部區域或區外通知用戶點擊? (我想在不同的情況下顯示不同的觀點)

是否可以添加一個偵聽器來通知點擊?

這裏是我的代碼尖晶石:

private void displayNotificationMessage(String message, boolean vibrate, boolean playSound, Intent contentIntent, String notificationTag) 
{  

    Notification notification = new Notification(R.drawable.buslogo, message, System.currentTimeMillis()); 

    PendingIntent myPendingIntent = PendingIntent.getActivity(this.getBaseContext(),0, contentIntent,PendingIntent.FLAG_UPDATE_CURRENT); 

    notification.setLatestEventInfo(this, "Friendly Reminder", message, myPendingIntent); 

    contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    if (vibrate) 
     notification.vibrate=new long[] {100L, 100L, 200L, 500L}; 

    if (playSound) 
     notification.defaults |= Notification.DEFAULT_SOUND; 


    notification.number = ++notificationCounter; 
    notificationMgr.notify(notificationTag, notificationCounter, notification); 

} 

@Override 
protected void onNewIntent(Intent intent) { 
    Log.i(TAG, "*********** onNewIntent(), intent = " + intent); 
    if (intent.getExtras() != null) 
    { 
     Log.i(TAG, "in onNewIntent = " + intent.getExtras().getString("test")); 
    } 
    super.onNewIntent(intent); 
    setIntent(intent); 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    Log.i(TAG, "*********** Main - onResume()"); 

    Intent intent = this.getIntent(); 
    if (intent.getExtras() != null) 
    { 
     Log.i(TAG, "test = " + intent.getExtras().getString("test")); 
    } 
} 

public void createNotification(String msnContent) 
{ 
    Intent intent = new Intent(); 
    intent.setClass(this, Main.class); 
    Bundle bundle = new Bundle(); 
    bundle.putString("test", msnContent); 
    intent.putExtras(bundle); 
    displayNotificationMessage(msnContent, true, true, intent, "test"); 

} 

回答

2

是否有可能增加一個監聽器通知卡嗒一聲?

對於每種情況使用不同的PendingIntent。如果您只是更改額外功能,請務必在創建PendingIntent時使用FLAG_UPDATE_CURRENT

+0

謝謝,我修改了我的代碼。每次在通過notificationMgr.notify()發送通知之前,我都會將一個新的PendingIntent對象傳遞給notification.setLatestEventInfo()。但它仍然無法區分通知欄上的哪個通知已被點擊。由於onResume()總是返回最新的setExtra()值。 – 2011-05-24 16:39:54

+0

是否有任何方式將價值/對象從原始活動傳遞給通知所調用的意圖? – 2011-05-24 16:42:44

+0

@ user767987:「由於onResume()總是返回最新的setExtra()值。」請參閱http://stackoverflow.com/questions/6089698/get-the-intent-that-woke-up-my-activity/6089739#6089739 – CommonsWare 2011-05-24 16:47:09

0

我發現這個帖子的解決方案 - Confusing behavior when starting new activity with PendingIntent。此外,感謝CommonsWare的幫助。

我缺少intent.setAction()。

下面是檢測到通知欄的通知已被點擊的代碼spinet。希望它能幫助有相同問題的其他人。

package com.NotificationTest; 

import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class Main extends Activity { 
    private static final String TAG = Main.class.getSimpleName(); 
    int notificationCounter =0; 
    private NotificationManager notificationMgr; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     notificationMgr =(NotificationManager)getSystemService(
       NOTIFICATION_SERVICE); 

     notificationMgr.cancelAll(); 

     Button b1 = (Button) findViewById(R.id.b1); 
     b1.setText("B1"); 
     b1.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) { 
       createNotification("From B1"); 
      } 
     }); 


     Button b2 = (Button) findViewById(R.id.b2); 
     b2.setText("B2"); 
     b2.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) { 
       createNotification("From B2"); 
      } 
     }); 
    } 


    private void displayNotificationMessage(String message, boolean vibrate, boolean playSound, Intent contentIntent, String notificationTag) 
    {  

     Notification notification = new Notification(R.drawable.icon, message, System.currentTimeMillis()); 


     contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     PendingIntent myPendingIntent = PendingIntent.getActivity(this.getBaseContext(),0, contentIntent,PendingIntent.FLAG_UPDATE_CURRENT); 


     notification.setLatestEventInfo(this, "Friendly Reminder", message, myPendingIntent); 



     /*if (vibrate) 
      notification.vibrate=new long[] {100L, 100L, 200L, 500L}; 

     if (playSound) 
      notification.defaults |= Notification.DEFAULT_SOUND;*/ 


     notification.number = ++notificationCounter; 
     notificationMgr.notify(notificationTag, notificationCounter, notification); 

    } 

    @Override 
    protected void onNewIntent(Intent intent) { 
     Log.i(TAG, "*********** onNewIntent(), intent = " + intent); 
     if (intent.getExtras() != null) 
     { 
      Log.i(TAG, "in onNewIntent = " + intent.getExtras().getString("test")); 
     } 
     super.onNewIntent(intent); 
     setIntent(intent); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     Log.i(TAG, "*********** Main - onResume()"); 

     Intent intent = this.getIntent(); 
     if (intent.getExtras() != null) 
     { 
      String extraOfClickedNotification = intent.getExtras().getString("test"); 

      Log.i(TAG, "test = " + extraOfClickedNotification); 

      if (extraOfClickedNotification.equals("From B1")) 
      { 
       // Do something 
      } 
      else if (extraOfClickedNotification.equals("From B1")) 
      { 
       // Do something 
      } 
     } 
    } 

    public void createNotification(String msgContent) 
    { 
     Intent intent = new Intent(); 
     intent.setAction(msgContent + System.currentTimeMillis()); 
     intent.setClass(this, Main.class); 
     Bundle bundle = new Bundle(); 
     bundle.putString("test", msgContent); 
     intent.putExtras(bundle); 
     displayNotificationMessage(msgContent, true, true, intent, "test"); 

    } 

}