onesignal で通知を正常に送信できます。すべての機能が正常に動作しています。しかし、今日、アクションボタンを通知に実装しようとすると、機能しません。ID「ActionOne」のアクションボタンを追加しようとしました。通知の下にあるこのアクションボタンをクリックすると、必要なテキストを共有するための共有インテントが開始されます。しかし、それを行うたびにアプリがクラッシュします。ただし、ID「ActionTwo」の別のアクションボタンに単純なトーストを追加しようとすると、機能しました。これから私を助けてください。ここにコードがあります -
public class MyNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
@Override
public void notificationOpened(OSNotificationOpenResult result) {
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String open;
if (data != null) {
open = data.optString("open", null);
if (open != null && open.equals("activitytwo")) {
Log.i("OneSignalExample", "customkey set with value: " + open);
Intent intent = new Intent(MyApplication.getContext(), Activitytwo.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
MyApplication.getContext().startActivity(intent);
} else {
Intent intent = new Intent(MyApplication.getContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
MyApplication.getContext().startActivity(intent);
}
}
if (actionType == OSNotificationAction.ActionType.ActionTaken){
Log.i("OneSignalExample", "Button pressed with id: " + result.action.actionID);
if (result.action.actionID.equals("ActionOne")) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBodyText = "hello";
sharingIntent.putExtra(Intent.EXTRA_SUBJECT,"");
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBodyText);
MyApplication.getContext().startActivity(Intent.createChooser(sharingIntent, "Share via"));
}else if (result.action.actionID.equals("ActionTwo")) {
Toast.makeText(MyApplication.getContext(), "ActionTwo Button was pressed", Toast.LENGTH_LONG).show();
}
}
}
}
通知が受信されたときのlogcatは次のとおりです-
11-16 18:57:47.266 26067-26067/com.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapplication, PID: 26067
java.lang.RuntimeException: Unable to start receiver com.onesignal.NotificationOpenedReceiver: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2851)
at android.app.ActivityThread.access$1700(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1547)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:1372)
at android.app.ContextImpl.startActivity(ContextImpl.java:1359)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:323)
at com.myapplication.MyNotificationOpenedHandler.notificationOpened(MyNotificationOpenedHandler.java:52)
at com.onesignal.OneSignal$15.run(OneSignal.java:1486)
at com.onesignal.OSUtils.runOnMainUIThread(OSUtils.java:204)
at com.onesignal.OneSignal.fireNotificationOpenedHandler(OneSignal.java:1483)
at com.onesignal.OneSignal.runNotificationOpenedCallback(OneSignal.java:1432)
at com.onesignal.OneSignal.handleNotificationOpen(OneSignal.java:1519)
at com.onesignal.NotificationOpenedProcessor.processIntent(NotificationOpenedProcessor.java:116)
at com.onesignal.NotificationOpenedProcessor.processFromContext(NotificationOpenedProcessor.java:51)
at com.onesignal.NotificationOpenedReceiver.onReceive(NotificationOpenedReceiver.java:38)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2844)
at android.app.ActivityThread.access$1700(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1547)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)