2016-01-22 112 views
0

這是我的AndroidManifest.xml:如何在android studio中使用OneSignal?

<?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) 

由於我在應用程序文件中,我不能做activity.this。 請幫忙。

回答

0

我認爲this SO answer應該適用於您的情況。特別是以下部分:

u可以使用應用程序上下文由呼叫 之前加入創建對話框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();