2017-01-20 2 views
0

Ich versuche eine Aktivität zu starten, indem ich auf eine Benachrichtigung klicke, aber das funktioniert nur, wenn ich bereits in der App bin.Starten einer Aktivität beim Klicken auf eine Benachrichtigung

Hier ist der Code, den ich denke, mit ihm zu tun hat:

private void sendNotification(String body) { 

    Intent intent = new Intent(ImpFirebaseMessagingService.this, codeActivity.class); 
    intent.putExtra("body", body); 
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); 

    Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle("Implementation TFA") 
      .setContentText(body) 
      .setAutoCancel(true) 
      .setSound(notificationSound) 
      .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(0, notificationBuilder.build()); 
} 
+0

Wird dieser Code in einem Dienst geschrieben? – shadygoneinsane

+0

@shadygoneinsane Ich folgte einem Tutorial und ich denke schon, weil die Datei MyFirebaseMessagingService heißt –

Antwort

1

Folgende Arbeiten in meiner App:

private void sendReminderNotification(Context context) { 
    Resources resources = context.getResources(); 
    Intent notificationIntent = new Intent(context, TitleActivity.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder builder = 
      new NotificationCompat.Builder(context) 
        .setSmallIcon(android.R.drawable.ic_lock_idle_alarm) 
        .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.my_diary_launcher_icon)) 
        .setContentTitle("MyDiary") 
        .setContentText("Reminder to input entry.") 
        .setDefaults(Notification.DEFAULT_ALL) 
        .setAutoCancel(true) 
        .setPriority(NotificationCompat.PRIORITY_MAX) 
        .setContentIntent(contentIntent); 

    // Add as notification 
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    manager.notify(NOTIFICATION_ID, builder.build()); 
} 
0

Verwenden pendingIntent Daten von intent weitergeben müssen:

private void sendReminderNotification(Context context) { 

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
     .setSmallIcon(R.mipmap.ic_launcher) 
     .setContentTitle("Implementation TFA") 
     .setContentText(body) 
     .setAutoCancel(true) 
     .setSound(notificationSound); 

Intent intent = new Intent(ImpFirebaseMessagingService.this, codeActivity.class); 
intent.putExtra("body", body); 
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT); 
notificationBuilder.setContentIntent(pendingIntent); 

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
notificationManager.notify(0, notificationBuilder.build()); 
} 
Verwandte Themen