2013-07-09 36 views

回答

0

通過創建PendingIntent來實現這一點。
例如,這將在單擊通知時啓動MainActivity。

Intent intent = new Intent(this, MainActivity.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); 

Notification noti = new Notification.Builder(this) 
     .setContentTitle("Notication title") 
     .setContentText("Content text") 
     .setContentIntent(pendingIntent).build(); 
+0

抱歉,這不起作用,它會從活動中創建新實例 – user1796624

+0

然後您可以將PendingIntent.getActivity()更改爲PendingIntent.getBroadcast(),並使您的IntentReceiver接受這個意圖,並做你想做的任何事情。 – Mine

1

實施pending intent
代碼

Intent pi = new Intent(); 
pi.setClass(getApplicationContext(), youactivity.class); 
// The PendingIntent to launch our activity if the user selects this notification 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,pi, PendingIntent.FLAG_UPDATE_CURRENT); 
String msgText = mMessage; 
// construct the Notification object. 
Notification notif = new Notification(R.drawable.icon, msgText,System.currentTimeMillis()); 

清單

<activity android:name="com.InfoActivity" android:noHistory="false android:excludeFromRecents="false"></activity> 
0

我不知道你在談論谷歌雲端通訊與否。但是,如果這是一個正常的通知,那麼在創建通知時,您必須提供Pending Intent。只需將您想要的課程放入待定意圖中,以便當用戶單擊該通知時,您將被驅動到您所謂的應用程序。片段:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, 
     "Your application activity".class), PendingIntent."Flag you want"); 

之後,在創建通知時使用此意圖。它的方法是setContentIntent(「上面的意圖」)並通過NotificationManager激發你的通知!

+0

需要添加以下標誌intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER);給啓動器意圖以啓動應用程序,並 – user1796624

+0

感謝提醒! :) – thegiga

1

這是complite代碼,我在這裏找到了答案Bring application to front after user clicks on home button

Intent intent = new Intent(ctx, SplashScreen.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      intent.setAction(Intent.ACTION_MAIN); 
      intent.addCategory(Intent.CATEGORY_LAUNCHER); 

      PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, 
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), 
        PendingIntent.FLAG_CANCEL_CURRENT); 

      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
        ctx).setContentTitle(extras.getString("title")) 
        .setContentText(extras.getString("message")) 
        .setSmallIcon(R.drawable.ic_launcher) 
        .setContentIntent(contentIntent); 
      Notification noti = mBuilder.build(); 
      noti.flags = Notification.DEFAULT_LIGHTS 
       | Notification.FLAG_AUTO_CANCEL; 
      mNotificationManager.notify(NOTIFICATION_ID, noti); 

重要的事情是對意圖的標誌,這將打開的應用程序,或者打開把它前面,或者什麼也不做您在瀏覽應用程序時點擊通知

相關問題