2017-07-09 62 views
0

我有一個通知,當我點擊它時,它只是關閉,應用程序不會回到視圖中。上傳不通過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()); 

    } 
} 

在我的清單我有這個。

<receiver 
      android:name=".NotificationReceiver" /> 

我錯過了什麼?

謝謝!

回答

2

您應該創建新的Intent來打開活動,而不是來自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