如何使用 OneSignal 打开特定的 activity?
How to open a specific activity using OneSignal?
我正在使用 onesignal 发送推送通知。
当用户单击推送通知时,我使用此示例代码打开特定的 activity。
如果我想打开另一个特定的activity,我应该怎么做?
package com.moho.app;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;
import org.json.JSONObject;
public class MyNotificationOpenedHandler 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 activityToBeOpened;
String activity;
//While sending a Push notification from OneSignal dashboard
// you can send an addtional data named "activityToBeOpened" and retrieve the value of it and do necessary operation
//If key is "activityToBeOpened" and value is "AnotherActivity", then when a user clicks
//on the notification, AnotherActivity will be opened.
//Else, if we have not set any additional data MainActivity is opened.
if (data != null) {
activityToBeOpened = data.optString("activityToBeOpened", null);
if (activityToBeOpened != null && activityToBeOpened.equals("AnotherActivity")) {
Log.i("OneSignalExample", "customkey set with value: " + activityToBeOpened);
Intent intent = new Intent(MainMenu.getContext(), AboutUs.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
MainMenu.getContext().startActivity(intent);
} else if (activityToBeOpened != null && activityToBeOpened.equals("MainActivity")) {
Log.i("OneSignalExample", "customkey set with value: " + activityToBeOpened);
Intent intent = new Intent(MainMenu.getContext(), MainMenu.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
MainMenu.getContext().startActivity(intent);
} else {
Intent intent = new Intent(MainMenu.getContext(), MainMenu.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
MainMenu.getContext().startActivity(intent);
}
}
}
这将是您 ApplicationClass.java 的代码:
package com.application;
import android.app.Application;
import android.content.Intent;
import android.util.Log;
import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;
import org.json.JSONObject;
@SuppressWarnings("unused")
public class ApplicationClass extends Application
{
@Override
public void onCreate()
{
super.onCreate();
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.setNotificationOpenedHandler(new NotificationOpenedHandler())
.unsubscribeWhenNotificationsAreDisabled(true)
.init();
}
public class NotificationOpenedHandler implements OneSignal.NotificationOpenedHandler
{
@Override
public void notificationOpened(OSNotificationOpenResult result)
{
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String activityToBeOpened;
String activity;
if (data != null)
{
activityToBeOpened = data.optString("activityToBeOpened", null);
if (activityToBeOpened != null && activityToBeOpened.equals("ABC"))
{
Log.i("OneSignal", "customkey set with value: " + activityToBeOpened);
Intent intent = new Intent(ApplicationClass.this, ABCActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
} else if (activityToBeOpened != null && activityToBeOpened.equals("DEF"))
{
Intent intent = new Intent(ApplicationClass.this, DEFActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
}
}
}
}
自定义代码以满足您的需要(更改数据的值和 class 的名称)。
我试图设置一个空的附加数据来打开 MainActivity,但是,这对我来说不起作用。可能是因为我定义了:
<meta-data
android:name="com.onesignal.NotificationOpened.DEFAULT"
android:value="DISABLE"/>
在 AndroidManifest.xml 中。所以,我想,如果需要,您还必须设置另一个附加数据值才能打开 MainActivity。只需在已经包含的那些之后添加另一个 else if
以继续添加更多要启动的键和活动。
如果您要使用所有这些意图,请确保在您的 AndroidManifest.xml 中没有为 android:launchMode
设置任何您将使用通知启动的活动.基本上不应该存在
此外,要使它正常工作,您必须将 AndroidManifest.xml 更改为如下内容:
<application
android:name=".ApplicationClass"
android:allowBackup="true"
android:fullBackupContent="@xml/backup_descriptor"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="AllowBackup">
此代码中为使通知正常工作而必须执行的唯一部分是 android:name=".ApplicationClass"
。
希望对您有所帮助。
我正在使用 onesignal 发送推送通知。 当用户单击推送通知时,我使用此示例代码打开特定的 activity。 如果我想打开另一个特定的activity,我应该怎么做?
package com.moho.app;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;
import org.json.JSONObject;
public class MyNotificationOpenedHandler 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 activityToBeOpened;
String activity;
//While sending a Push notification from OneSignal dashboard
// you can send an addtional data named "activityToBeOpened" and retrieve the value of it and do necessary operation
//If key is "activityToBeOpened" and value is "AnotherActivity", then when a user clicks
//on the notification, AnotherActivity will be opened.
//Else, if we have not set any additional data MainActivity is opened.
if (data != null) {
activityToBeOpened = data.optString("activityToBeOpened", null);
if (activityToBeOpened != null && activityToBeOpened.equals("AnotherActivity")) {
Log.i("OneSignalExample", "customkey set with value: " + activityToBeOpened);
Intent intent = new Intent(MainMenu.getContext(), AboutUs.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
MainMenu.getContext().startActivity(intent);
} else if (activityToBeOpened != null && activityToBeOpened.equals("MainActivity")) {
Log.i("OneSignalExample", "customkey set with value: " + activityToBeOpened);
Intent intent = new Intent(MainMenu.getContext(), MainMenu.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
MainMenu.getContext().startActivity(intent);
} else {
Intent intent = new Intent(MainMenu.getContext(), MainMenu.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
MainMenu.getContext().startActivity(intent);
}
}
}
这将是您 ApplicationClass.java 的代码:
package com.application;
import android.app.Application;
import android.content.Intent;
import android.util.Log;
import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;
import org.json.JSONObject;
@SuppressWarnings("unused")
public class ApplicationClass extends Application
{
@Override
public void onCreate()
{
super.onCreate();
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.setNotificationOpenedHandler(new NotificationOpenedHandler())
.unsubscribeWhenNotificationsAreDisabled(true)
.init();
}
public class NotificationOpenedHandler implements OneSignal.NotificationOpenedHandler
{
@Override
public void notificationOpened(OSNotificationOpenResult result)
{
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String activityToBeOpened;
String activity;
if (data != null)
{
activityToBeOpened = data.optString("activityToBeOpened", null);
if (activityToBeOpened != null && activityToBeOpened.equals("ABC"))
{
Log.i("OneSignal", "customkey set with value: " + activityToBeOpened);
Intent intent = new Intent(ApplicationClass.this, ABCActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
} else if (activityToBeOpened != null && activityToBeOpened.equals("DEF"))
{
Intent intent = new Intent(ApplicationClass.this, DEFActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
}
}
}
}
自定义代码以满足您的需要(更改数据的值和 class 的名称)。
我试图设置一个空的附加数据来打开 MainActivity,但是,这对我来说不起作用。可能是因为我定义了:
<meta-data
android:name="com.onesignal.NotificationOpened.DEFAULT"
android:value="DISABLE"/>
在 AndroidManifest.xml 中。所以,我想,如果需要,您还必须设置另一个附加数据值才能打开 MainActivity。只需在已经包含的那些之后添加另一个 else if
以继续添加更多要启动的键和活动。
如果您要使用所有这些意图,请确保在您的 AndroidManifest.xml 中没有为 android:launchMode
设置任何您将使用通知启动的活动.基本上不应该存在
此外,要使它正常工作,您必须将 AndroidManifest.xml 更改为如下内容:
<application
android:name=".ApplicationClass"
android:allowBackup="true"
android:fullBackupContent="@xml/backup_descriptor"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="AllowBackup">
此代码中为使通知正常工作而必须执行的唯一部分是 android:name=".ApplicationClass"
。
希望对您有所帮助。