无法从通知中打开活动
Posted
技术标签:
【中文标题】无法从通知中打开活动【英文标题】:unable to open an activity from notifications 【发布时间】:2020-04-28 22:53:50 【问题描述】:我正在发出打开活动的通知,并在单击时发送附加信息,但我使用所有方式,但当我单击它时它总是打开我的启动器活动, 我的代码是这样的
public class AlarmNotificationReceiver extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
sendNotification(context);
public void sendNotification(Context context)
int NOTIFICATION_ID = 234;
String CHANNEL_ID = "my_channel_01";
Intent intent = new Intent(context, TestActivity.class);
intent.putExtra("title", "Today,s Dish");
intent.putExtra("getDetailShowId", 50);
intent.setAction("MyIntent");
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
// String CHANNEL_ID = "my_channel_01";
CharSequence name = "Title";
String Description = "Its new";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]100, 200, 300, 400, 500, 400, 300, 200, 400);
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText("Its old")
.setContentIntent(contentIntent)
.setAutoCancel(true)
;
Intent resultIntent = new Intent(context, MenuActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
notificationManager.notify(NOTIFICATION_ID, builder.build());
我没有使用奥利奥,所以算了吧,我得到了这样的额外功能,但是这段代码不起作用它总是打开我的启动活动
toolbartitle = getIntent().getStringExtra("title");
detailShowUrl = getIntent().getStringExtra("getDetailShowId");
【问题讨论】:
【参考方案1】:TL;DR
创建通知渠道 创建通知 在通知中附加待处理的意图,您将在其中添加额外数据 点击通知后,pendingIntent 将启动一个活动 根据待处理的 Intent 额外数据处理已启动的 Activity 内部将发生的情况执行上述步骤的一些代码/资源:
如何创建通知渠道:
Android O - Notification Channels and NotificationCompat
如何创建一个pendingIntent:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("data", json.toString());
intent.putExtra("has_extra_data", true);
PendingIntent pendingIntent = PendingIntent.getActivity(this, json.getInt("id") /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);
如何在通知中插入pendingIntent并显示:
notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("title")
.setContentText("description")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id/* ID of notification */, notificationBuilder.build());
现在将它添加到 MainActivity 以处理通知的数据:
@Override
protected void onCreate(Bundle savedInstanceState)
if (getIntent().getExtras().getBoolean("has_extra_data"))
String data = getIntent().getExtras().getString("data");
// Do what you want ...
// You may want to start a new activity or show something in UI
根据您的问题记录:
如果您想打开除 MainActivity 之外的另一个活动,您应该通过在您的 pendingIntent extras 中设置自定义变量并让 MainActivity 将您重定向到另一个 Activity 来实现。本质上,MainActivity 充当路由器!
【讨论】:
以上是关于无法从通知中打开活动的主要内容,如果未能解决你的问题,请参考以下文章
无法从 Google Play 控制台中的实时开发人员通知发送测试通知