2017-07-09 1 views
0

J'ai une notification que lorsque je tape dessus, il se ferme simplement, l'application n'est pas remise en vue.Contexte ne passant pas à NotificationReceiver (BroadcastReceiver)

Ceci est mon MainActivity -

intention Intention = new Intent (getApplicationContext(), NotificationReceiver.class); intention.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); 

Alors le NotificationReceiver ressemble à ceci -

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

    } 
} 

Dans mon Manifest je cela.

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

Que manque-t-il?

Merci!

Répondre

2

Vous devez créer une nouvelle intention pour ouvrir l'activité, au lieu de l'intention existante provenant de 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

Fonctionne très bien. Je vous remercie. (Acceptera dès que possible) – AndyCr15