2017-07-09 12 views
0

私はそれをタップすると、それは単に閉じると、アプリが表示に戻って来ていないという通知があります。Context not NotificationReceiver(BroadcastReceiver)に渡さない

これは私のMainActivityである -

テントの意図=新しいテント(getApplicationContext()、NotificationReceiver.class)。intent.putExtra( "Message"、notificationText);

  PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

      AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
      alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

その後NotificationReceiverはこのようになります -

public class NotificationReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
       | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     String notificationText = intent.getStringExtra("Message"); 
     //if we want ring on notification then uncomment below line 
//  Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context) 
       .setContentIntent(pendingIntent) 
       .setSmallIcon(R.drawable.rr) 
       .setContentTitle("Check your reminders!") 
       .setContentText(notificationText) 
       .setAutoCancel(true); 

     notificationManager.notify(100, builder.build()); 

    } 
} 

私のマニフェストでは、私はこれを持っています。

私は何が欠けていますか?

ありがとうございます!

答えて

2

onReceiveから来ている既存のインテントではなく、新しいインテントを作成してアクティビティを開く必要があります。

public class NotificationReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 



      String notificationText = intent.getStringExtra("Message"); 
      //if we want ring on notification then uncomment below line 
//  Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

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

      PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

      NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context) 
        .setContentIntent(pendingIntent) 
        .setSmallIcon(R.drawable.rr) 
        .setContentTitle("Check your reminders!") 
        .setContentText(notificationText) 
        .setAutoCancel(true); 

      notificationManager.notify(100, builder.build()); 

     } 
    } 
+0

素晴らしいです。ありがとうございました。 (できるだけ早く受け入れます) – AndyCr15

関連する問題