使用GCM,Freshplanet ANE,Flash Professional CS6和 VB.NET 推送通知



我创建了一个Flash CS6 AIR for Android应用程序,其中包含用于推送通知的Freshplanet ANE。一切似乎都正常,但我的设备上仍然没有收到通知。

这是我的Flash CS6代码:

import com.freshplanet.nativeExtensions.PushNotification;
import com.freshplanet.nativeExtensions.PushNotificationEvent;
txtMsg.text = 'starting..';
var push:PushNotification = PushNotification.getInstance();
if (push.isPushNotificationSupported) {
    push.registerForPushNotification("XXXXXXXXXXXXX"); //Google project ID
}
push.addEventListener(PushNotificationEvent.PERMISSION_GIVEN_WITH_TOKEN_EVENT, onRegistered);
push.addEventListener(PushNotificationEvent.PERMISSION_REFUSED_EVENT, onRefused);
function onRegistered(event:PushNotificationEvent):void
{
    txtMsg.appendText("Registered with registration id:" + event.token);
}
function onRefused(event:PushNotificationEvent):void
{
    txtMsg.appendText("Refused:" + event.errorMessage);
}

这段代码似乎有效,因为当我在设备上启动我的应用程序时,我的 txtMsg 字段会显示 event.token。一个长字符串,包含我猜测是唯一的设备 ID。

我的安卓清单如下所示: `

<android>
    <manifestAdditions>
      <![CDATA[<manifest>
                <uses-permission android:name="android.permission.INTERNET"/>
                <uses-permission android:name="android.permission.WAKE_LOCK"/>
                <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
                <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
                <uses-permission android:name="android.permission.GET_ACCOUNTS" />
                <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
                <uses-permission android:name="air.it.test.PushTest2.permission.C2D_MESSAGE" />
                <permission android:name="air.it.test.PushTest2.permission.C2D_MESSAGE" android:protectionLevel="signature" />
                <application>
                    <activity android:name="air.it.test.PushTest2"></activity>
                    <receiver android:name="com.freshplanet.nativeExtensions.C2DMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
                        <!-- Receive the actual message -->
                        <intent-filter>
                            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                            <category android:name="air.it.test.PushTest2" />
                        <intent-filter>
                        </intent-filter>
                            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                            <category android:name="air.it.test.PushTest2" />
                        </intent-filter>
                    </receiver>
                    <service android:name="com.freshplanet.nativeExtensions.LocalNotificationService"/>
                    <receiver android:name="com.freshplanet.nativeExtensions.LocalBroadcastReceiver" android:process=":remote"></receiver>
                </application>
</manifest>]]>
    </manifestAdditions>
  </android>
  <extensions>
    <extensionID>com.freshplanet.AirPushNotification</extensionID>
  </extensions>

'

这是我的 VB.NET 服务器应用程序,它使用上面收到的令牌将消息发送到设备。

Dim regId As String = "XXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXX" device token id
    Dim MessageText As String = "Hope this works"
    Dim applicationID As String = "APP ID" 'received from Google API console as the Key for server applications
    Dim result As String = ""
    Dim SENDER_ID As String = "XXXXXXXXXXXXX" 'same ID used in my actionscript file above - Google project ID
    Dim httpWebRequest As WebRequest = WebRequest.Create("https://android.googleapis.com/gcm/send")
    httpWebRequest.ContentType = "application/json"
    httpWebRequest.Method = "POST"
    httpWebRequest.Headers.Add(String.Format("Authorization: key={0}", applicationID))
    httpWebRequest.Headers.Add(String.Format("Sender: key={0}", SENDER_ID))
    Dim streamWriter As StreamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
    Dim json As String = "{""registration_ids"": [""" & regId & """], ""data"": {""message"": """ & MessageText & """}}"
    Response.Write(json)
    streamWriter.Write(json)
    streamWriter.Flush()
    streamWriter.Close()
    Dim httpResponse As WebResponse = httpWebRequest.GetResponse()
    Dim streamReader As StreamReader = New StreamReader(httpResponse.GetResponseStream())
    result = streamReader.ReadToEnd()
    Response.Write(result)

当我运行此函数时,它会收到一条成功消息。所以一切似乎都正常工作,但我的设备没有收到任何东西。我做错了什么?谢谢

Fresh Planet有很多分支,试试这个最近的分支:

https://github.com/freshplanet/ANE-Push-Notification/tree/feature/famepop-withOptions

您需要更改 JASON 以发送这些值:

contentTitle   <---- this is the title
contentText    <---- this is the message
tickerText     <---- this is showed in the task bar
这个

push.addEventListener(PushNotificationEvent.COMING_FROM_NOTIFICATION_EVENT, onPushMessage);

最新更新