这是我的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.volleybd.tsp.onesignalfuck">
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
android:name=".Tapplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的申请文件:
public class Tapplication extends Application {
@Override
public void onCreate() {
super.onCreate();
OneSignal.startInit(this)
.setNotificationOpenedHandler(new NotificationOpenedHandler())
.init();
}
// This fires when a notificaiton is opened by tapping on it or one is received while the app is runnning.
private class NotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
@Override
public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
try {
String messageTitle;
AlertDialog.Builder builder = null;
if (additionalData != null) {
if (additionalData.has("discount"))
messageTitle = "Discount!";
else if (additionalData.has("bonusCredits"))
messageTitle = "Bonus Credits!";
else
messageTitle = "Other Extra Data";
builder = new AlertDialog.Builder(getApplicationContext())
.setTitle(messageTitle)
.setMessage(message + "\n\n" + additionalData.toString());
}
else if (isActive) // If a push notification is received when the app is being used it does not display in the notification bar so display in the app.
builder = new AlertDialog.Builder(getApplicationContext())
.setTitle("OneSignal Message")
.setMessage(message);
// Add your app logic around this so the user is not interrupted during gameplay.
if (builder != null)
builder.setCancelable(true)
.setPositiveButton("OK",null)
.create().show();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
当我点击一下推送通知时,我得到的是LogCat日志:
01-23 02:15:41.318 19727-19727/com.volleybd.tsp.onesignal W/System.err: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
01-23 02:15:41.318 19727-19727/com.volleybd.tsp.onesignal W/System.err: at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
01-23 02:15:41.318 19727-19727/com.volleybd.tsp.onesignal W/System.err: at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)
01-23 02:15:41.318 19727-19727/com.volleybd.tsp.onesignal W/System.err: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
01-23 02:15:41.329 19727-19727/com.volleybd.tsp.onesignal W/System.err: at android.app.Dialog.show(Dialog.java:319)
01-23 02:15:41.329 19727-19727/com.volleybd.tsp.onesignal W/System.err: at com.volleybd.tsp.onesignal.Tapplication$ExampleNotificationOpenedHandler.notificationOpened(Tapplication.java:51)
01-23 02:15:41.329 19727-19727/com.volleybd.tsp.onesignal W/System.err: at com.onesignal.OneSignal.fireNotificationOpenedHandler(OneSignal.java:891)
01-23 02:15:41.329 19727-19727/com.volleybd.tsp.onesignal W/System.err: at com.onesignal.OneSignal.runNotificationOpenedCallback(OneSignal.java:886)
01-23 02:15:41.329 19727-19727/com.volleybd.tsp.onesignal W/System.err: at com.onesignal.OneSignal.handleNotificationOpened(OneSignal.java:914)
01-23 02:15:41.329 19727-19727/com.volleybd.tsp.onesignal W/System.err: at com.onesignal.NotificationOpenedProcessor.processIntent(NotificationOpenedProcessor.java:88)
01-23 02:15:41.329 19727-19727/com.volleybd.tsp.onesignal W/System.err: at com.onesignal.NotificationOpenedProcessor.processFromActivity(NotificationOpenedProcessor.java:52)
01-23 02:15:41.329 19727-19727/com.volleybd.tsp.onesignal W/System.err: at com.onesignal.NotificationOpenedReceiver.onReceive(NotificationOpenedReceiver.java:11)
01-23 02:15:41.329 19727-19727/com.volleybd.tsp.onesignal W/System.err: at android.app.ActivityThread.handleReceiver(ActivityThread.java:2725)
01-23 02:15:41.329 19727-19727/com.volleybd.tsp.onesignal W/System.err: at android.app.ActivityThread.-wrap14(ActivityThread.java)
01-23 02:15:41.329 19727-19727/com.volleybd.tsp.onesignal W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
01-23 02:15:41.329 19727-19727/com.volleybd.tsp.onesignal W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
01-23 02:15:41.329 19727-19727/com.volleybd.tsp.onesignal W/System.err: at android.os.Looper.loop(Looper.java:148)
01-23 02:15:41.330 19727-19727/com.volleybd.tsp.onesignal W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
01-23 02:15:41.330 19727-19727/com.volleybd.tsp.onesignal W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-23 02:15:41.330 19727-19727/com.volleybd.tsp.onesignal W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-23 02:15:41.330 19727-19727/com.volleybd.tsp.onesignal W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
因为我在申请文件中,所以我不能做活动。这个。 请帮忙。
答案 0 :(得分:0)
我认为this SO answer应该适用于您的情况。特别是以下部分:
您可以使用应用程序上下文通过在调用之前添加来创建对话框 到Dialog.show();
Dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
所以在你的情况下:
Dialog dialog = builder.setCancelable(true).setPositiveButton("OK",null).create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();