[英]Firebase - Activity restarts when user clicks notification
我正在编写一个使用 firebase 来实现通知的应用程序。 在我的 MainActivity 中,我有一个带有一些 url 的 WebView,但问题是当用户单击通知时,我想在 WebView 中使用不同的 url 打开 MainActiviy。 我已经阅读了很多内容,并且已经向意图(单击通知时打开 MainActivity)添加了一个包,其中包含所需的 url。 但是当我点击通知时,MainActivity 重新启动,我的意思是,它不会转到 onNewIntent,而是运行 onCreate。 我是这样实现的:
private void sendNotification(String messageTitle, String messageBody, String url){
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//This adds the url to the intent
if(!url.equals("")){
Bundle bundle = new Bundle();
bundle.putString("url", url);
intent.putExtras(bundle);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
NotificationCompat.Builder notificationBuilder = new NotificationCompat
.Builder(this, channelId)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel(channelId,
"Notification channel",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());
}
onNewIntent 我有这个:
Bundle bundle = intent.getExtras();
if (bundle != null) {
String url = bundle.getString("url");
mWebView.loadUrl(url);
}
但是当点击通知时,活动只是重新启动,所以它不会在NewIntent上运行,并且日志给了我这个错误:
02-08 12:51:12.140 19056-19056/com.example.android.app E/ActivityThread: Activity com.example.android.app.MainActivity has leaked IntentReceiver com.example.android.app.MainActivity$1@d7818db that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity com.example.android.app.MainActivity has leaked IntentReceiver com.example.android.app.MainActivity$1@d7818db that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:999)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:795)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1329)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1309)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1303)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:554)
at com.example.android.app.MainActivity.onCreate(MainActivity.java:264)
at android.app.Activity.performCreate(Activity.java:6367)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2511)
at android.app.ActivityThread.access$900(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1375)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5621)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
我在stackoverflow上读到一个类似的问题是取消注册一个BroadCastReceiver来修复这个错误,但我对如何去做有点迷茫。
我已经尝试将 sendNotification 的意图更改为
Intent intent = new Intent("android.intent.action.MAIN");
但在这种情况下,当用户单击通知时,它什么也不做。
有谁知道如何修复它以便在用户点击时加载 url? 提前谢谢你
从Android 文档中它说明了这一点:
如果它已经声明它的启动模式为“multiple”(默认)并且你没有在同一个意图中设置FLAG_ACTIVITY_SINGLE_TOP
,那么它将被完成并重新创建; 对于所有其他启动模式,或者如果FLAG_ACTIVITY_SINGLE_TOP
被设置,那么这个 Intent 将被传递到当前实例的 onNewIntent()。
所以看起来像在创建新意图时设置FLAG_ACTIVITY_SINGLE_TOP
标志应该解决并运行 onNewIntent() 方法,而不是重新创建应用程序。
创建一个这样的方法
private PendingIntent retrievePlaybackAction(final String action) {
Intent intent = new Intent(action);
return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
现在像这样添加你的意图
builder.setContentIntent(retrievePlaybackAction("OPEN_MAIN")); //change action text as you want
创建一个我猜你已经创建的简单类NotificationReceiver
,现在从你发送通知的地方创建这个类的对象
private NotificationReceiver notificationReceiver;
在onCreate()
注册您的接收器
notificationReceiver = new NotificationReceiver();
IntentFilter intentFilterNextClick = new IntentFilter("OPEN_MAIN");
registerReceiver(notificationReceiver, intentFilterNextClick);
//can create exception, better to surround with try catch
在onDestroy()
取消注册您的接收器
unregisterReceiver(notificationReceiver);
//can create exception, better to surround with try catch
要打开或执行某些操作,请将其添加到您的接收器中
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "onReceive: received " + intent.getAction());
String action = intent.getAction();
//no need to create switch you can also use if
switch (action) {
case "OPEN_MAIN":
openMain();
break;
}
}
//here is openMain();
private void openMain(Context context) {
Intent openMainIntent = new Intent(context, MainActivity.class);
openMainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //this flag is important
context.startActivity(openMainIntent);
}
如果应用程序被最小化或关闭,这也将起作用
希望这会有所帮助!
请询问您是否需要更多帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.