2017-07-09 4 views
0

Ich habe eine Benachrichtigung, dass wenn ich darauf tippen, schließt es einfach, die Anwendung nicht wieder in Ansicht gebracht wird.Context vorbei nicht NotificationReceiver (BroadcastReceiver)

Dies ist in meinem MainActivity -

Intent Absicht = new Intent (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); 

Dann sieht die NotificationReceiver so -

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()); 

    } 
} 

In meinem Manifest ich das haben.

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

Was fehlt mir?

Danke!

Antwort

2

sollten Sie neue Intent erstellen, die Aktivität zu öffnen, anstatt Absicht der bestehenden, die von OnReceive kommt.

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

Arbeiten groß. Vielen Dank. (Wird akzeptieren, sobald ich kann) – AndyCr15

Verwandte Themen