gpt4 book ai didi

android - 点击通知启动 Activity

转载 作者:行者123 更新时间:2023-11-29 19:38:39 33 4
gpt4 key购买 nike

我希望在单击通知时启动主 Activity 。即使用户不在应用程序中也是如此。我该怎么做?

NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.diamond)
.setContentTitle("Crystallise")
.setContentText("making your thoughts crystal clear");
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notifyID, mBuilder.build());

最佳答案

首先你需要PendingIntent :

Intent intent=new Intent(mContext, Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent notificationIntent= PendingIntent.getActivity(mContext,requestCode, intent,PendingIntent.FLAG_UPDATE_CURRENT);

然后在您的 NotificationCompat.Builder 中添加:.setContentIntent(notificationIntent)

对于唯一的请求代码,使用这个:int requestCode=(int)System.currentTimeMillis();

您也可以在非 Activity 类中使​​用它,例如:Service、BroadcastReceiver。然后,即使处于关闭状态,您的应用也会启动。

关于android - 点击通知启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38933333/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com