单击通知不会打开 Android 应用程序
Posted
技术标签:
【中文标题】单击通知不会打开 Android 应用程序【英文标题】:Clicking on notification doesn't open Android app 【发布时间】:2018-09-24 22:12:40 【问题描述】:我正在使用 onesignal 和 firebase 将通知从 wordpress 博客推送到 android 应用程序,当我单击刚刚到达的通知时,应用程序只有在后台运行时才会打开。如果它完全关闭,单击通知将无济于事。即使应用不在后台打开,如何实现点击通知打开应用?
以下是处理通知的代码:
class nyonNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler
// This fires when a notification is opened by tapping on it.
@Override
public void notificationOpened(OSNotificationOpenResult result)
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String customKey;
if (data != null)
customKey = data.optString("customkey", null);
if (customKey != null)
Log.i("OneSignalnyon", "customkey set with value: " + customKey);
if (actionType == OSNotificationAction.ActionType.ActionTaken)
Log.i("OneSignalnyon", "Button pressed with id: " + result.action.actionID);
// The following can be used to open an Activity of your choice.
// Replace - getApplicationContext() - with any Android Context.
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
【问题讨论】:
您是否尝试过删除意图标志? 我刚试过,如果完全关闭了还是打不开。 待定意图? @notTdar 未决意图呢? 在通知构建期间,您不需要待定意图来打开活动、服务.. 【参考方案1】:我用于在点击通知时打开应用程序的代码,运行良好:
Intent resultIntent = new Intent(getApplicationContext(), YourActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//if you want to send some data
resultIntent.putExtra(AppConstants.NOTIFICATION, data);
现在你必须创建 PendingIntent PendingIntent:根据文档,通过创建待定意图意味着您授予它执行您指定的操作的权利,就像其他应用程序是您自己一样。 https://developer.android.com/reference/android/app/PendingIntent
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
现在,当您创建通知时,将此待处理的意图 setContentIntent(resultPendingIntent) 设置为该通知。
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setStyle(inboxStyle)
.setWhen(getTimeMilliSec(System.currentTimeMillis() + ""))
.setSmallIcon(R.drawable.ic_app_icon)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), icon))
.setContentText(message)
.setChannelId(CHANNEL_ID)
.build();
【讨论】:
我在“PendingIntent.getBroadcast”上犯了错误,而我必须使用“PendingIntent.getActivity”:) 我对此发表了评论,以防有人遇到像我这样的问题。【参考方案2】:要解决这个问题,首先是更清楚地阅读文档(我没有做的)所以这里是:
默认情况下,OneSignal 将在点击通知时打开或恢复您的启动器活动。您可以通过在 AndroidManifest.xml 中的应用程序标签内添加元数据标签 com.onesignal.NotificationOpened.DEFAULT 设置为 DISABLE 来禁用此行为强>.
确保在 android manifest 中注册它,例如:
<application ...>
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
</application>
为打开的通知创建处理程序,例如:
public class MyNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler
private final Context context;
public MyNotificationOpenedHandler(Context context)
this.context = context;
@Override
public void notificationOpened(OSNotificationOpenResult result)
if (result.action.type == OSNotificationAction.ActionType.Opened)
JSONObject data = result.notification.payload.additionalData;
if (data == null)
return;
String category = data.optString("category", null);
if (category == null)
return;
if (category.equals("global"))
Intent intent = new Intent(context, NotificationDetailsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
下一点是:
确保您在 Application 类的 onCreate 方法中使用 setNotificationOpenedHandler 初始化 OneSignal。您需要从此回调中调用 startActivity。
您需要扩展 Application 类,例如:
public class MyApplication extends Application
@Override
public void onCreate()
super.onCreate();
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.setNotificationOpenedHandler(new MyNotificationOpenedHandler(getApplicationContext()))
.unsubscribeWhenNotificationsAreDisabled(true)
.init();
在 android manifest 中设置应用名称,例如:
<application
android:name=".MyApplication"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme">
您已准备好在应用关闭时处理通知。
【讨论】:
【参考方案3】:设置“setNotificationOpenedHandler”
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.setNotificationOpenedHandler(new NotificationOpenedHandler())
.init();
将此类添加到您的启动器活动中(确保通知具有“additionalData”)
public class NotificationOpenedHandler implements OneSignal.NotificationOpenedHandler
// This fires when a notification is opened by tapping on it.
@Override
public void notificationOpened(OSNotificationOpenResult result)
//OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String customKey;
if (data != null)
customKey = data.optString("Data", null);
if (customKey != null)
Log.d("LOGGED", "notificationOpened: " + customKey);
if(customKey.equals("Notification"))
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
else
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
//Toast.makeText(MainActivity.this, "Value is : " + customKey, Toast.LENGTH_SHORT).show();
更多信息 https://documentation.onesignal.com/docs/android-native-sdk#section--notificationopenedhandler-
【讨论】:
在回答之前阅读问题。我使用了文档中的代码。你只是粘贴了同样的东西。 我记得当我发布这个时,没有问题的 sn-p 代码。不知道,可能是我忽略了。没关系。 :) 但这对我有用。【参考方案4】:将以下内容添加到您的 AndroidManifest.xml 以防止启动您的主 Activity
<application ...>
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
</application>
【讨论】:
以上是关于单击通知不会打开 Android 应用程序的主要内容,如果未能解决你的问题,请参考以下文章