Android Firebase-当用户单击通知时,活动重新启动
我正在编写一个使用firebase实现通知的应用程序。在我的MainActivity中,我有一个带有url的WebView,但问题是当用户单击通知时,我想在WebView中使用不同的url打开MainActivity。我读了很多书,并在intent中添加了一个bundle(单击通知时打开MainActivity),该bundle包含所需的url。但当我点击通知时,MainActivity会重新启动,我的意思是,它不会转到onNewIntent,而是运行onCreate。我就是这样实现的:Android Firebase-当用户单击通知时,活动重新启动,android,firebase,Android,Firebase,我正在编写一个使用firebase实现通知的应用程序。在我的MainActivity中,我有一个带有url的WebView,但问题是当用户单击通知时,我想在WebView中使用不同的url打开MainActivity。我读了很多书,并在intent中添加了一个bundle(单击通知时打开MainActivity),该bundle包含所需的url。但当我点击通知时,MainActivity会重新启动,我的意思是,它不会转到onNewIntent,而是运行onCreate。我就是这样实现的: pri
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());
}
我有这个:
Bundle bundle = intent.getExtras();
if (bundle != null) {
String url = bundle.getString("url");
mWebView.loadUrl(url);
}
但当单击通知时,活动将重新启动,因此它不会运行OnEvent,并且日志会显示以下错误:
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)
但是在这种情况下,当用户单击通知时,它什么也不做
有人知道如何修复它,以便在用户单击时加载url吗?
提前向您表示感谢来自it声明:
如果它已将其启动模式声明为“多个”(默认),并且您没有按照相同的意图设置FLAG\u ACTIVITY\u SINGLE\u TOP
,则它将完成并重新创建;对于所有其他启动模式,或者如果设置了FLAG\u ACTIVITY\u SINGLE\u TOP
,则此意图将传递到当前实例的onNewIntent()
因此,在创建新意图时设置
标志\u ACTIVITY\u SINGLE\u 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);
}
要打开或执行某些操作,请将此添加到接收器中
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);
}
如果应用程序最小化或关闭,这也会起作用
希望这会有帮助
如果您需要更多帮助,请询问 很高兴它只是这么小的东西:)祝你在应用程序中的冒险事业好运!