【发布时间】:2018-06-05 18:28:16
【问题描述】:
我正在学习如何在 Android 设备上处理 OneSignal 推送通知。问题是,当应用程序关闭时,当我收到通知时,即使我定义了必要的功能(我猜)它仍然会打开“Splash Activity”,它在清单中定义为 MAIN LAUNCHER。我需要打开一个包含有效负载数据的不同活动。我在创建这些代码时引用的链接是this,我在this answer 上看到了引用。因为这个项目是分类的,所以我只显示相关代码。
这是我的清单文件。
<application
android:name="packageName.CustomAppName"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT"
android:value="DISABLED"/>
<activity
android:name="anotherPackageName.SplashActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="anotherPackageName.PaymentActivity"
android:screenOrientation="portrait" />
<service
android:name="somepackagename.NotificationsForPayment"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE">
<intent-filter>
<action android:name="com.onesignal.NotificationExtender" />
</intent-filter>
</service>
</application>
这是我定义 OneSignal 服务的应用程序类。
public class CustomAppName extends Application {
private static CustomAppName instance;
public static CustomAppName getInstance() {
return instance;
}
public void onCreate() {
super.onCreate();
OneSignal.startInit(this)
.setNotificationOpenedHandler(new CustomNotificationOpening())
.init();
instance = this;
}
}
这是我的 CustomNotificationOpening 类。
public class CustomNotificationOpening implements OneSignal.NotificationOpenedHandler {
@Override
public void notificationOpened(OSNotificationOpenResult notification) {
notification.notification.payload.additionalData.names();
JSONObject data = notification.notification.payload.additionalData;
Intent intent = new Intent(CustomAppName.getInstance(), PaymentActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("paymentModel", data);
CustomAppName.getInstance().startActivity(intent);
}
这是我的 NotificationsForPayment 类,它从 NotificationExtenderService 扩展而来。
public class NotificationsForPayment extends NotificationExtenderService {
@Override
protected boolean onNotificationProcessing(OSNotificationReceivedResult notification) {
NotificationExtenderService.OverrideSettings overrideSettings = new NotificationExtenderService.OverrideSettings();
overrideSettings.extender = new NotificationCompat.Extender() {
@Override
public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
// Sets the background notification color to Red on Android 5.0+ devices.
Bitmap icon = BitmapFactory.decodeResource(CustomAppName.getInstance().getResources(),
R.drawable.ic_os_notification_fallback_white_24dp);
builder.setLargeIcon(icon);
return builder.setColor(new BigInteger("FF0000FF", 16).intValue());
}
};
OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
}
我真的不知道我在哪里做错了。当应用程序打开时,当我单击通知时,我可以看到“notificationOpened”功能正在触发。但是当它关闭时,由于我无法调试程序并且通知会打开启动活动,我知道我该问这个问题了,因为我找到的答案都没有。有没有办法在应用程序关闭时使用通知中的特定数据打开其他活动?感谢您的帮助,非常感谢。
【问题讨论】:
-
你的有效载荷是什么样的?
-
只有“PaymentId”键下的字符串进来。
-
但它是数据负载吗?通知?还是两者兼而有之?
-
它是“additionalData”,所以我假设一个数据有效负载。只有一个成员的“PaymentId”中有一个值。
标签: android push-notification onesignal