解析 Unity 推送示例不起作用 [英] Parse Unity Push Sample not working

查看:15
本文介绍了解析 Unity 推送示例不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力在我的 Unity android 应用程序上使用 Parse.已经工作了几天,但还没有成功.无论我多么努力,我都无法为 Android 注册我的设备令牌,这是向 android 发送推送通知所必需的.下面是我的示例代码和清单片段..

I am struggling of using Parse on my Unity android application. Have been working on several days but no success yet. No matter how hard I try, I couldn't get my device token registered for Android which is needed for sending push notifications to android. Below is my sample code and manifest snippet..

  <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.
            }

推荐答案

Parse Unity Push Sample not working",同意!!男孩,这样的痛苦(Y)

"Parse Unity Push Sample not working", agree!! boy, such a pain in the ( Y )

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

I've been fighting with Unity Parse Pushes for days(a week?) and I just solve it. I am so, so, happy, I want to share my happiness (first time in my life I write at StackOverflow, guess it was about time...)

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

Not sure if this will solve your problem, but for sure you have something wrong at this line:

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

我们必须针对您当前的活动更改 "currentActivity".在我的情况下,我写了这个:

We have to change "currentActivity" for what ever is your current activity. In my case I have written this:

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

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

Because that's the name of my activity in my 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>

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

Still, it might not solve your problem. So, just in case, I copy paste my code!

AndroidManifest.xml:

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- 包标识符,以防 thetnswe 将替换为 com.JoyDash.Movies

Please, replace com.XXXX.YYYY by -your- bundle identifier, in case of thetnswe would be by com.JoyDash.Movies

(有趣的是:没有 GCM BroadcastReceiver 正如 Parse Push 示例所说的那样,它在吐出这样的错误 使用 parse.com 和 android studio 设置推送通知时出错)

(funny thing: there is no GCM BroadcastReceiver as the Parse Push samples says, it was spitting errors like this error setting up push notification using parse.com and android studio)

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

And then I have a C# Unity Script in one of my GameObjects (actually the same than the one I use to initialize 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
    }
}

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

Hope it helps! If not, let me know... and we will see :)

这篇关于解析 Unity 推送示例不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆