Parse platform 解析Unity推送示例不工作

Parse platform 解析Unity推送示例不工作,parse-platform,Parse Platform,我很难在我的Unity android应用程序上使用Parse。已经工作了好几天,但还没有成功。无论我怎么努力,我都无法为Android注册设备令牌,这是向Android发送推送通知所需的。下面是我的示例代码和清单片段 <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK

我很难在我的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.
            }

#如果统一
NotificationServices.RegisterForRemoteNotificationTypes(RemoteNotificationType.Alert|
RemoteNotificationType.Badge|
RemoteNotificationType.Sound);
#恩迪夫
//ParsePush.SubscribeAsync
Debug.Log(“使用解析推送进行注册:”+Application.platform);
ParsePush.ParsePushNotificationReceived+=(发送方,参数)=>
{
if(Application.platform==RuntimePlatform.Android)
{
AndroidJavaClass parseUnityHelper=新的AndroidJavaClass(“com.parse.parseUnityHelper”);
AndroidJavaClass unityPlayer=新的AndroidJavaClass(“com.unity3d.player.unityPlayer”);
AndroidJavaObject currentActivity=unityPlayer.GetStatic(“currentActivity”);
//调用默认行为。
Log(“从Unity调用解析,有效负载为:“+args.Payload”);
parseUnityHelper.CallStatic(“handleParsePushNotificationReceived”,currentActivity,args.Payload.ToString());
//parseUnityHelper.CallStatic(“handleParsePushNotificationReceived”,currentActivity,args.Payload.StringPayload);
}
else if(Application.platform==RuntimePlatform.IPhonePlayer)
{
//IDictionary payload=args.payload;
//foreach(有效负载中的字符串键)
//{
//Log(“有效载荷:“+key+”:“+Payload[key].ToString());
//}
}
};
//解析安装
Log(“设备令牌:+ParseInstallation.CurrentInstallation.DeviceToken”);
if(ParseInstallation.CurrentInstallation!=null&&!string.IsNullOrEmpty(ParseInstallation.CurrentInstallation.DeviceToken))
{
Log(“设备令牌:+ParseInstallation.CurrentInstallation.DeviceToken”);
}
其他的
{
//创建新的解析安装
//ParseInstallation.CurrentInstallation.SaveAsync().ContinueWith(t=>
//    {
//如果(t.IsFaulted | | t.IsCanceled)
//        {
//Log(“解析安装未能保存”);
//        }
//否则
//        {
//Log(“解析安装已成功保存”);
//        }
//    });
//ParseInstallation.cre
Log(“没有收到此设备的安装数据..正在订阅通道”);
ParsePush.SubscribeAsync(“Channel01”).ContinueWith(t=>
{
如果(t.IsFaulted | | t.IsCanceled)
{
Log(“订阅推送通知失败”);
}
其他的
{
Log(“已成功订阅推送通知”);
}
});
//安装。
}
“解析Unity推送示例不工作”,同意!!天哪,这是多么痛苦的事啊

我已经和Unity抗争了好几天(一周?),我只是解决了它。我是如此,如此,快乐,我想分享我的快乐(我有生以来第一次在StackOverflow写作,我想是时候了…)

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

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

然后在我的一个游戏对象中有一个C#Unity脚本(实际上与我用来初始化解析的脚本相同):

使用UnityEngine;
使用系统集合;
使用语法分析;
公共类注册:MonoBehavior{
//用于初始化
无效开始(){
#如果你是安卓
ParsePush.ParsePushNotificationReceived+=(发送方,参数)=>{
AndroidJavaClass parseUnityHelper=新的AndroidJavaClass(“com.parse.parseUnityHelper”);
AndroidJavaClass unityPlayer=新的AndroidJavaClass(“com.unity3d.player.unityPlayer”);
AndroidJavaObject currentActivity=unityPlayer.GetStatic(“com.unity3d.player.UnityPlayerActivity”);
//调用默认行为。
parseUnityHelper.CallStatic(“handleParsePushNotificationReceived”,currentActivity,args.StringPayload);
};              
#恩迪夫
}
}

希望有帮助!如果没有,请告诉我。。。我们将看到:)

你让它工作了吗?@thetns你能解决这个问题吗?没有。。我还没有解决这个问题。在发布之后,我又推了一次,但没有成功!我简直不敢相信。我一直在等待,看看会发生什么,然后给出反馈。问题是,只有当Unity应用程序不作为recente应用程序出现在后台时,它才起作用。如果它在那里,或者在前台,通知将不会显示(实际上,最后一种情况是正常的)。仍在努力解决这个问题。
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject> ("com.unity3d.player.UnityPlayerNativeActivity");
  <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>
<?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>
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
    }
}