有人可以告诉我,我错过了什么。我有一个webview应用程序。它工作正常除了这个,当我关闭应用程序信号通知到达罚款,但当我点击通知应用程序无法打开。以下是与此问题相关的一些代码。 我一直在寻找解决方案,但我曾经尝试过它不起作用。 当应用程序运行时,通知出现并打开正常,但当它关闭时它不会启动应用程序。 感谢。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.unsubscribeWhenNotificationsAreDisabled(true)
.setNotificationOpenedHandler(new
OneSignal.NotificationOpenedHandler() {
@Override
public void notificationOpened(OSNotificationOpenResult
result) {
String launchURL =
result.notification.payload.launchURL;
Intent intent = new Intent(getApplicationContext(),
NotificationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
| Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("url", launchURL);
startActivity(intent);
}
})
.init();
}
public class NotificationExtender extends NotificationExtenderService {
@Override
protected boolean onNotificationProcessing(OSNotificationReceivedResult notification) {
OverrideSettings overrideSettings = new OverrideSettings();
overrideSettings.extender = new NotificationCompat.Extender() {
@Override
public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
// Sets the background notification color to Green on Android 5.0+ devices.
return builder.setColor(new BigInteger("#005aaa", 16).intValue())
.setSmallIcon(R.drawable.ic_stat_onesignal_default);
}
};
OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
Log.d("OneSignalExample", "Notification displayed with id: " + displayedResult.androidNotificationId);
return true;
}
}
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".NotificationExtender"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false">
<intent-filter>
<action android:name="com.onesignal.NotificationExtender" />
</intent-filter>
</service>
<activity
android:name=".NotificationActivity"
android:parentActivityName=".MainActivity"/>
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
答案 0 :(得分:0)
我解决了。 它接缝你不需要NotificationOpenedHandler,只是NotificationExtender扩展NotificationExtenderService。它看起来像这样:
public class NotificationExtender extends NotificationExtenderService {
public NotificationExtender() {
super();
}
@Override
protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
Intent intent = new Intent(getApplicationContext(), NotificationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("url", receivedResult.payload.launchURL);
PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_stat_onesignal_default)
.setSound(uri)
.setContentTitle(receivedResult.payload.title)
.setContentText(receivedResult.payload.body)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_onesignal_large_icon_default))
.setColor(getResources().getColor(R.color.logo_color))
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
return true;
}
}