2013-10-28 35 views
0

有沒有辦法在用戶點擊通知ui中的通知時重定向到另一個活動?在android中捕獲通知

例如

我創建谷歌日曆事件,當有一個通知,當用戶點擊它,它會打開我的活動,而不是直接去谷歌日曆。

+0

看看[這](http://stackoverflow.com/a/13716784/896038),我想這就是你需要的。 –

回答

0

試試這個:

private void generateNotification(Context context, String message, 
long when, String query) { 
int icon = R.drawable.icon; 
long when = System.currentTimeMillis(); 
String appname = context.getResources().getString(R.string.app_name); 
NotificationManager notificationManager = (NotificationManager) context 
    .getSystemService(Context.NOTIFICATION_SERVICE); 
int currentapiVersion = android.os.Build.VERSION.SDK_INT; 
Notification notification; 

Intent intent = new Intent(context, MainActivity.class); 
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
    intent, 0); 

if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) { 
    notification = new Notification(icon, message, when); 
    notification.setLatestEventInfo(context, appname, message, 
      contentIntent); 
    notification.flags = Notification.FLAG_AUTO_CANCEL; 
    notificationManager.notify((int) when, notification); 

} else { 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
      context); 
    notification = builder.setContentIntent(contentIntent) 
      .setSmallIcon(icon).setTicker(appname).setWhen(when) 
      .setAutoCancel(true).setContentTitle(appname) 
      .setContentText(message).build(); 

    notificationManager.notify((int) when, notification); 

} 

希望這有助於。