解析Unity推送样本不起作用

时间:2015-05-30 17:04:57

标签: parse-platform

我正在努力在我的Unity android应用程序上使用Parse。已经工作了几天但还没有成功。无论我怎么努力,我都无法获得为Android发送推送通知所需的Android设备令牌。以下是我的示例代码和清单摘要..

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.VIBRATE" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />
  <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
  <permission android:protectionLevel="signature"      android:name="com.JoyDash.Movies.permission.C2D_MESSAGE" />
  <uses-permission android:name="com.JoyDash.Movies.permission.C2D_MESSAGE" />


 <!--Parse Push notification receiver-->
    <service android:name="com.parse.PushService" />
    <receiver android:name="com.parse.ParseBroadcastReceiver">
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.USER_PRESENT" />
      </intent-filter>
    </receiver>

    <receiver android:name="com.JoyDash.Movies.Receiver" android:exported="false">
      <intent-filter>
        <action android:name="com.parse.push.intent.RECEIVE" />
        <action android:name="com.parse.push.intent.DELETE" />
        <action android:name="com.parse.push.intent.OPEN" />
      </intent-filter>
    </receiver>

    <receiver android:name="com.parse.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
      <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <category android:name="com.JoyDash.Movies" />
      </intent-filter>
    </receiver>

    <!--<receiver android:name="com.parse.ParsePushBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
      <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <category android:name="com.JoyDash.Movies" />
      </intent-filter>
    </receiver>

    <service android:name="com.parse.ParsePushService" />-->


#if UNITY_IOS
        NotificationServices.RegisterForRemoteNotificationTypes (RemoteNotificationType.Alert |
                                                                 RemoteNotificationType.Badge |
                                                                 RemoteNotificationType.Sound);
#endif

            //ParsePush.SubscribeAsync
            Debug.Log("Registration with Parse Push. : " + Application.platform);
            ParsePush.ParsePushNotificationReceived += (sender, args) =>
            {
                if (Application.platform == RuntimePlatform.Android)
                {
                    AndroidJavaClass parseUnityHelper = new AndroidJavaClass("com.parse.ParseUnityHelper");
                    AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                    AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

                    // Call default behavior.
                    Debug.Log("Calling Parse from Unity and Payload is : " + args.Payload);
                    parseUnityHelper.CallStatic("handleParsePushNotificationReceived", currentActivity, args.Payload.ToString());
                    //parseUnityHelper.CallStatic("handleParsePushNotificationReceived", currentActivity, args.Payload.StringPayload);
                }
                else if (Application.platform == RuntimePlatform.IPhonePlayer)
                {
                    //IDictionary<string, object> payload = args.Payload;

                    //foreach (string key in payload) 
                    //{
                    //    Debug.Log("Payload: " + key + ": " + payload[key].ToString());
                    //}
                }

            };

            //Parse Installation
            Debug.Log("Device Token : " + ParseInstallation.CurrentInstallation.DeviceToken);
            if (ParseInstallation.CurrentInstallation != null && !string.IsNullOrEmpty(ParseInstallation.CurrentInstallation.DeviceToken))
            {
                Debug.Log("Device Token : " + ParseInstallation.CurrentInstallation.DeviceToken);
            }
            else
            {
                //Create a new parse installation
                //ParseInstallation.CurrentInstallation.SaveAsync().ContinueWith(t =>
                //    {
                //        if (t.IsFaulted || t.IsCanceled)
                //        {
                //            Debug.Log("Parse installation failed to save.");
                //        }
                //        else
                //        {
                //            Debug.Log("Parse installation saved successfully.");
                //        }
                //    });

                //ParseInstallation.cre
                Debug.Log("There is no installation data received for this device.. Now subscribing to a channel");
                ParsePush.SubscribeAsync("Channel01").ContinueWith(t =>
                {
                    if (t.IsFaulted || t.IsCanceled)
                    {
                        Debug.Log("Subscription of push notification failed.");
                    }
                    else
                    {
                        Debug.Log("Push notification subscribed successfully.");
                    }
                });


                //installation.
            }

1 个答案:

答案 0 :(得分:2)

“Parse Unity Push Sample无效”,同意!男孩,(Y)这样的痛苦

我已经和Unity Parse Pushes战斗了好几天(一周?),我就解决了。我是如此,所以,快乐,我想分享我的快乐(我生命中第一次在StackOverflow写作,猜测它是关于时间......)

不确定这是否可以解决您的问题,但确定您在这一行有问题:

 AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

我们必须更改“currentActivity”以了解您当前的活动。在我的情况下,我写了这个:

AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject> ("com.unity3d.player.UnityPlayerNativeActivity");

因为这是我在AndroidManifest.xml中的活动名称

  <activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:launchMode="singleTask" android:screenOrientation="landscape">
      <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>

但是,它可能无法解决您的问题。所以,为了以防万一,我复制粘贴我的代码!

的AndroidManifest.xml:

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="preferExternal" package="com.XXXX.YYYY" android:versionName="1" android:versionCode="7">
  <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
  <uses-feature android:glEsVersion="0x00020000" />
  <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
  <uses-permission android:name="android.permission.VIBRATE" />
  <uses-permission android:name="android.permission.GET_ACCOUNTS" />
  <uses-permission android:name="android.permission.GET_TASKS" />  
  <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />  
  <permission android:protectionLevel="signature"  android:name="com.XXXX.YYYY.permission.C2D_MESSAGE" />
  <uses-permission android:name="com.XXXX.YYYY.permission.C2D_MESSAGE" />   
  <uses-permission android:name="com.android.vending.CHECK_LICENSE" /> 
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />



  <application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false">

    <service android:name="com.parse.ParsePushService" />


    <receiver android:name="com.parse.ParsePushBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
      <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />              
        <action android:name="com.parse.push.intent.DELETE" />
        <action android:name="com.parse.push.intent.OPEN" />
        <category android:name="com.XXXX.YYYY" />
      </intent-filter>
    </receiver>

    <activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:launchMode="singleTask" android:screenOrientation="landscape">
      <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" />

    <!-- replace @drawable/push_icon with your push icon identifier -->
    <meta-data android:name="com.parse.push.notification_icon" android:resource="@drawable/app_icon"/>  
  </application>
</manifest>

请将 com.XXXX.YYYY 替换为-your-bundle标识符,如果 thetnswe 将由 com.JoyDash.Movies

(有趣的是:没有GCM BroadcastReceiver,因为Parse Push样本说,它是像这样的随地吐痰错误error setting up push notification using parse.com and android studio

然后我在我的一个GameObjects中有一个C#Unity脚本(实际上和我用来初始化Parse的那个相同):

using UnityEngine;
using System.Collections;
using Parse;


public class ParsePushRegistration : MonoBehaviour {
    // Use this for initialization
    void Start () {
        #if UNITY_ANDROID
        ParsePush.ParsePushNotificationReceived += (sender, args) => {                  
            AndroidJavaClass parseUnityHelper = new AndroidJavaClass ("com.parse.ParseUnityHelper");
            AndroidJavaClass unityPlayer = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
            AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject> ("com.unity3d.player.UnityPlayerNativeActivity");

            // Call default behavior.
            parseUnityHelper.CallStatic ("handleParsePushNotificationReceived", currentActivity, args.StringPayload);
        };              
        #endif
    }
}

希望它有所帮助!如果没有,请告诉我......我们会看到:)