单击通知时启动活动

时间:2017-01-20 10:03:02

标签: java android

我尝试通过点击通知来启动活动,但只有在我已经在应用中时才会生效。

以下是我认为与之相关的代码:

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

2 个答案:

答案 0 :(得分: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());
}

答案 1 :(得分:0)

使用pendingIntent传递intent的数据:

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