3

我通過NotificationManagerBroadcastReceiver顯示通知,但如果用戶觸摸通知,則不會發生任何情況。如果我從視圖創建通知,那麼在觸摸通知時,包含的活動將處於最前面。Android:如何對通知觸摸事件做出反應?

如何顯示特定活動/視圖或以其他方式反應用戶觸摸通知?

+0

使用'ADB logcat',DDMS,或在Eclipse中DDMS角度看logcat的看到,當您輕觸個顯示哪些信息e'通知'。 – CommonsWare 2011-03-18 17:21:30

回答

3

當您從NotificationManager接收到廣播消息時,您必須創建一個新的PendingIntent,當用戶觸摸正確的通知時將觸發該PendingIntent。現在,PendingIntent必須有一個內部Intent,它將執行一個動作,例如啓動一個活動。

對在廣播接收器調用一個輔助方法你「的onReceive」的方法,如「showProximityAlertNotification」

@Override 
     public void onReceive(Context context, Intent intent) { 
showProximityAlertNotification(); 
} 


//now when the user touch the notification on the notification bar, an activity named //"ItemListActivity" will be launched. You can put an IntentFilter, a Category, an Action 
//to perform any different operations within your activity 
private void showProximityAlertNotification(){ 

     String titulo = getString(R.string.notification_proximity_alerts_fired_title); 
     String tickerText = getString(R.string.notification_proximity_alerts_fired_ticker); 
     String contextText = getString(R.string.notification_proximity_alerts_fired_content); 
     int icon = R.drawable.wk_logo; 
     long when = System.currentTimeMillis(); 

     Notification notification = new Notification(icon, tickerText, when); 

     //define the actions to perform when user touch the notification 
     Intent launchApp = new Intent(getApplicationContext(), ItemListActivity.class); 
     launchApp.putExtra("com.xxxxxxx.xxxxxxxxx.bean.Item", "anyObjectYouWant"); 
     launchApp.setAction("VIEW_DETAILS_PROPERTY"); 
     PendingIntent launchNotification = PendingIntent.getActivity(getApplicationContext(), 0, launchApp, 0); 
     notification.setLatestEventInfo(getApplicationContext(), titulo, contextText, launchNotification); 

     notificationManager.notify(NOTIFICATION_ID_PROXIMITY_ALERT_FIRED, notification); 
    } 

如果您正在執行新推出的活動,並希望取消發佈通知,只是這樣做:

String notManagerName = Context.NOTIFICATION_SERVICE; 
       NotificationManager notificationManager = (NotificationManager) getSystemService(notManagerName); 
       notificationManager.cancel(ProximityAlerts.NOTIFICATION_ID_PROXIMITY_ALERT_FIRED); 

歡呼

-3

在你的清單文件添加此

android:name=".QuadDealsPushReceiver" 

<application android:label="@string/app_name" android:name=".QuadDealsPushReceiver" 
    android:theme="@style/MyTheme" android:debuggable="true"> 

然後創建新的活動,如果您觸摸通知會重定向說YourActivityname.class粘貼此code..now ..

public class QuadDealsPushReceiver extends Application { 
public static String apid=null; 
public void onCreate(){ 
    AirMail am = AirMail.getInstance(); 
    am.acceptPush(this, new PushReceiver() { 
     @Override 
     public void onReceive(String message, String payload){ 
      Log.d("push", "Got message '" + message +"' and payload '" + payload + "'"); 
     } 
     @Override 
     public void onClick(String message, String payload){ 
      Intent intent = new Intent("android.intent.action.MAIN"); 
      intent.setClass(QuadDealsPushReceiver.this, YourActivityname.class); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      QuadDealsPushReceiver.this.startActivity(intent); 
     } 
    }); 

    am.setAPIDReceiver(this, new APIDReceiver() { 
     @Override 
     public void onReceive(String apids, boolean valid){ 
      apid=apids; 
      if(valid){ 
       Log.d("push", "Got apid: " + apid); 
      } else { 
       Log.d("push", "Application registration invalid!"+ apid); 
      } 
     } 

     @Override 
     public void onAirMailInstallRefusal() { 
      QuadMain.register = false; 
      Log.d("push", "AirMail Install Refused!"); 
     } 
    }); 
} 

}

+3

@Venaktesh:我相信你把這個答案貼到了一個錯誤的問題上,因爲它與通知或OP詢問的任何其他內容沒有任何關係。 – CommonsWare 2011-03-18 17:20:02

相關問題