2017-01-20 11 views
0

通知をクリックしてアクティビティを開始しようとしていますが、これは既にアプリ内にあるときにのみ機能します。通知をクリックするとアクティビティを開始します

ここで私はそれに関係していると思うのコードです:

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

は、このコードはサービスに書かれていますか? – shadygoneinsane

+0

@shadygoneinsane私はチュートリアルに従いました。ファイル名はMyFirebaseMessagingService –

答えて

1

私のアプリでは、次の作品:

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

使用pendingIntentintentのデータを渡すために:

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()); 
} 
関連する問題