2016-08-13 67 views
1

我希望在單擊通知時啓動主活動。即使用戶不在應用程序中也是如此。我該怎麼做?單擊通知發起活動

NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this) 
         .setSmallIcon(R.drawable.diamond) 
         .setContentTitle("Crystallise") 
         .setContentText("making your thoughts crystal clear"); 
NotificationManager mNotificationManager = 
     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
mNotificationManager.notify(notifyID, mBuilder.build()); 

回答

1

,首先你需要PendingIntent

Intent intent=new Intent(mContext, Activity.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
PendingIntent notificationIntent= PendingIntent.getActivity(mContext,requestCode, intent,PendingIntent.FLAG_UPDATE_CURRENT); 

然後在你的NotificationCompat.Builder補充一點:.setContentIntent(notificationIntent)

對於唯一的請求的代碼使用:int requestCode=(int)System.currentTimeMillis();

您可以使用此非活動類,例如:Service,BroadcastReceiver。然後即使它處於關閉狀態,您的應用程序也會啓動。

1

//添加意圖和的PendingIntent開放活動

Intent attendanceIntent = new Intent(getApplicationContext(), Details.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, 
       attendanceIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.diamond) 
        .setContentTitle("Crystallise") 
        .setContentIntent(pendingIntent) 
        .setContentText("making your thoughts crystal clear"); 
    NotificationManager mNotificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(notifyID, mBuilder.build());