我正在编写一个使用Firebase实施通知的应用程序。在我的MainActivity中,我有一个带有某些URL的WebView,但事实是,当用户单击通知时,我想在WebView中使用不同的URL打开MainActiviy。我已经阅读了很多书,并且向意图(当单击通知时将打开MainActivity)添加了一个捆绑包,该捆绑包包含所需的网址。但是,当我单击通知时,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);
}
但是当单击通知时,活动仅重新启动,因此它不会在onNewIntent上运行,并且日志显示此错误:
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吗? 预先谢谢你
答案 0 :(得分:1)
在Android docs中声明:
如果它已将其启动模式声明为“多个”(默认),并且您未将FLAG_ACTIVITY_SINGLE_TOP
设置为相同的意图,则它将完成并重新创建;对于所有其他启动模式,或者如果设置了FLAG_ACTIVITY_SINGLE_TOP
,则此Intent将被传递到当前实例的onNewIntent()。
因此,看起来在创建新的Intent时设置FLAG_ACTIVITY_SINGLE_TOP
标志应该解决并运行onNewIntent()方法,而不是重新创建应用程序。
答案 1 :(得分:0)
创建这样的方法
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);
}
如果应用程序最小化或关闭,这也将起作用
希望这会有所帮助!
请询问是否需要更多帮助!