2017-12-31 29 views
1

私の受信者の1人にローカル通知が設定されています。通知のオンクリック特定のアクティビティを開きたい。以下は私のコードです。ローカル通知をクリックするとアクティビティが開きます

NotificationCompat.Builder builder = 
      (NotificationCompat.Builder) new NotificationCompat.Builder(context) 
        .setSmallIcon(R.drawable.ic_launcher) 
        .setContentTitle("Its Your day!!") 
        .setContentText(message) 
        .setAutoCancel(true); 

    Intent notificationIntent = new Intent(context, MainActivity.class); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

    builder.setContentIntent(contentIntent); 
    // Add as notification 
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    manager.notify(times, builder.build()); 

しかし、これは機能しません。通知をクリックするとステータスバーから通知が削除されます。私は何か不足していますか?

+0

あなたはアプリを閉じた後にしようとしたんでした。 MainActivityが有効な場合、SINGLE_TOPは 'onNewIntent()'にインテントを渡します。 – ADM

答えて

0

Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOPを「意図」にFlagと追加してください。以下のような -

notificationIntent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

また、通知のための一意のIDを提供します。以下のような - また、上の

int uniqueNotificationId = (int) (System.currentTimeMillis() & 0xfffffff); 
    PendingIntent contentIntent = PendingIntent.getActivity(context, uniqueNotificationId , notificationIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

-

manager.notify(uniqueNotificationId , builder.build()); 
+0

'PendingIntent.FLAG_UPDATE_CURRENT'は同じ' requestCode'でのみ意味があります。それぞれの 'PendingIntent'の一意のIDは毎回新しい' PendingIntent'を作成します。 –

1

これを試してみてください。

Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

の代わりに、この:

Intent notificationIntent = new Intent(context, MainActivity.class); 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 
     PendingIntent.FLAG_UPDATE_CURRENT); 

あなたの通知ビルダーコードの前に使用し、このコードを。

は今、これはフルバージョンです:

Intent intent = new Intent(this, MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

NotificationCompat.Builder builder = 
      (NotificationCompat.Builder) new NotificationCompat.Builder(context) 
        .setSmallIcon(R.drawable.ic_launcher) 
        .setContentTitle("Its Your day!!") 
        .setContentText(message) 
        .setAutoCancel(true) 
        .setContentIntent(pendingIntent); 

NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

notificationManager.notify(times, notificationBuilder.build()); 

は、これを使用してみてください。あなたが欲望の答えを得ることを願っています。ここ

UPDATE
は、サンプルプロジェクトです:https://github.com/ImtiazDipto01/SimpleNotification

+0

まだ動作していません –

+0

私の編集したバージョンを確認してください。 –

+0

@madhuriHR UPDATEセクションをチェックして、私はサンプルプロジェクトを追加しました。何か問題があれば教えてください。 –

関連する問題