Android 具有 PendingIntent 的通知的额外空值
Extra null for Android Notification with PendingIntent
我正在尝试在点击通知时向 Activity 发送一个 int Extra。当我点击通知时 activity 启动,但额外的总是空的。
查了很多类似的问题都没有帮我解决问题。 (我尝试使用 onNewIntent() 但从未调用过)。如何才能成功发送Extra?
这是创建意图的 BroadcastReceiver:
public class EventAlarmNearReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent nextIntent = new Intent().setClass(context, EventActivity.class);
nextIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
int id = 1234;
nextIntent.putExtra("Id", (long) id);
nextIntent.setAction("nearReceiverAction");
PendingIntent nextPendingIntent = PendingIntent.getActivity(context, 1022, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
String text = "Your " + intent.getStringExtra("Name") + " is starting soon. Please read your documents.";
NotificationsDeliver.getInstance().sendNotification(context, "Meeting Mate", text, nextPendingIntent);
}
}
这是发送通知的 NotificationsDeliver 方法:
public void sendNotification(@NonNull Context context, String title, String text, PendingIntent pendingIntent ) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_event_note_white_24dp)
.setContentTitle(title)
.setContentText(text)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVibrate(new long[] { 1000, 1000, 1000 })
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(999, builder.build());
}
这是事件的 onCreate 方法Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
long id = intent.getLongExtra("id", 0);
}
我猜这是因为你在投入和获得额外时使用了不同的 "id" 这个词;比较:
nextIntent.putExtra("Id", (long) id);
// ^^^^
对
long id = intent.getLongExtra("id", 0);
// ^^^^
尝试对两者使用相同的大小写。
我正在尝试在点击通知时向 Activity 发送一个 int Extra。当我点击通知时 activity 启动,但额外的总是空的。
查了很多类似的问题都没有帮我解决问题。 (我尝试使用 onNewIntent() 但从未调用过)。如何才能成功发送Extra?
这是创建意图的 BroadcastReceiver:
public class EventAlarmNearReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent nextIntent = new Intent().setClass(context, EventActivity.class);
nextIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
int id = 1234;
nextIntent.putExtra("Id", (long) id);
nextIntent.setAction("nearReceiverAction");
PendingIntent nextPendingIntent = PendingIntent.getActivity(context, 1022, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
String text = "Your " + intent.getStringExtra("Name") + " is starting soon. Please read your documents.";
NotificationsDeliver.getInstance().sendNotification(context, "Meeting Mate", text, nextPendingIntent);
}
}
这是发送通知的 NotificationsDeliver 方法:
public void sendNotification(@NonNull Context context, String title, String text, PendingIntent pendingIntent ) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_event_note_white_24dp)
.setContentTitle(title)
.setContentText(text)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVibrate(new long[] { 1000, 1000, 1000 })
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(999, builder.build());
}
这是事件的 onCreate 方法Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
long id = intent.getLongExtra("id", 0);
}
我猜这是因为你在投入和获得额外时使用了不同的 "id" 这个词;比较:
nextIntent.putExtra("Id", (long) id);
// ^^^^
对
long id = intent.getLongExtra("id", 0);
// ^^^^
尝试对两者使用相同的大小写。