2016-08-13 9 views
1

通知がクリックされたときにメインアクティビティを開始します。これは、ユーザーがアプリにいなくても可能です。どうすればいいですか?あなたがPendingIntentを必要とするすべてのクリック通知で活動を開始

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

まず:

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にこの追加:あなたはこの中に使用することができますint requestCode=(int)System.currentTimeMillis();

:ユニークな要求コードを使用する場合は、これを.setContentIntent(notificationIntent)

を非アクティビティクラス(例: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()); 
関連する問題