0

我想安裝一個基本的Xamarin應用程序與Android的推送通知。我跟着演示here,似乎一切順利。應用程序日誌顯示使用下面的代碼接收到註冊令牌:Xamarin谷歌雲消息傳遞不工作

using System; 
using Android.App; 
using Android.Content; 
using Android.Util; 
using Android.Gms.Gcm; 
using Android.Gms.Gcm.Iid; 

namespace HawkProject.Droid { 
[Service(Exported = false)] 
class RegistrationIntentService : IntentService 
{ 
    static object locker = new object(); 

    public RegistrationIntentService() : base("RegistrationIntentService") {  } 

    protected override void OnHandleIntent (Intent intent) 
    { 
     try 
     { 
      Log.Info ("RegistrationIntentService", "Calling InstanceID.GetToken"); 
      lock (locker) 
      { 
       var instanceID = InstanceID.GetInstance (this); 
       var token = instanceID.GetToken (
        "1015213323461", GoogleCloudMessaging.InstanceIdScope, null); 

       Log.Info ("RegistrationIntentService", "GCM Registration Token: " + token); 
       //SendRegistrationToAppServer (token); 
       //Subscribe (token); 
      } 
     } 
     catch (Exception e) 
     { 
      Log.Debug("RegistrationIntentService", "Failed to get a registration token"); 
      Log.Debug("RegistrationIntentService", e.Message); 

      return; 
     } 
    } 

    void SendRegistrationToAppServer (string token) 
    { 
     // Add custom implementation here as needed. 
    } 

    void Subscribe (string token) 
    { 
     var pubSub = GcmPubSub.GetInstance(this); 
     pubSub.Subscribe(token, "/topics/hawkglobal", null); 
    } 
} 
} 

應用程序日誌返回:

[RegistrationIntentService] Calling InstanceID.GetToken 
[RegistrationIntentService] GCM Registration Token: dz8yxsF61tc:APA91bFpHBPsJuGUN2Yh5Hv9CyuAgyIUP4qthhNE79rJUOvLH__ildlVOaP9Q-X1lBpDk69BDRbnzDl4kdEdJBjyudwiVfRZnVR4trQwcM8JZYIEqMNFiXAy61RSjD2MWnC2kmNLj1Ev 

我的清單如下:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="com.gcs.hawkproject" android:versionCode="1" android:versionName="start"> 
<uses-sdk android:minSdkVersion="15" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
<uses-permission android:name="com.gcs.hawkproject.permission.C2D_MESSAGE" /> 
<permission android:name="com.gcs.hawkproject.permission.C2D_MESSAGE" 
      android:protectionLevel="signature" /> 
<application android:label="Hawk" android:icon="@drawable/log"></application> 
<application android:label="RemoteNotifications" android:icon="@drawable/Icon"> 
    <receiver android:name="com.google.android.gms.gcm.GcmReceiver" 
       android:exported="true" 
       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.gcs.hawkproject" /> 
     </intent-filter> 
    </receiver> 
    <service 
     android:name="com.gcs.hawkproject.MyGcmListenerService" 
     android:exported="false" > 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
     </intent-filter> 
    </service> 
    <service 
     android:name="com.gcs.hawkproject.MyInstanceIDListenerService" 
     android:exported="false"> 
     <intent-filter> 
      <action android:name="com.google.android.gms.iid.InstanceID"/> 
     </intent-filter> 
    </service> 
</application> 
</manifest> 

一切似乎進展順利,但是當我嘗試發送消息時,我得到以下結果:

POST:

{ 
    "data" : { "message" : "This is a test" }, 
    "to" : "dz8yxsF61tc:APA91bFpHBPsJuGUN2Yh5Hv9CyuAgyIUP4qthhNE79rJUOvLH__ildlVOaP9Q-X1lBpDk69BDRbnzDl4kdEdJBjyudwiVfRZnVR4trQwcM8JZYIEqMNFiXAy61RSjD2MWnC2kmNLj1Ev" 
} 

響應

{ 
    "multicast_id":5139152955408512939, 
    "success":0,"failure":1,"canonical_ids":0, 
    "results": [{"error":"NotRegistered"}] 
} 

有什麼想法?我對Xamarin和GCM都很陌生,所以我在幾個小時內摸索得失敗。

我有演示中的其他代碼,以及我可以發佈如有必要。

在此先感謝。

+0

遇到相同問題。你解決了嗎? –

+0

你可能想看看這個線程:http://stackoverflow.com/questions/34513614/google-cloud-message-not-registered-failure-and-unsubscribe-best-practices –

回答

0

我建議嘗試以下方法:

(1)卸載並重新安裝應用程序,從GCM收到新的通知令牌,再次嘗試推到新的令牌。

(2)仔細檢查項目ID「1015213323461」,是否有可能sender_id與用於發送通知的服務器證書不匹配?

(3)com.gcs.hawkproject是否與您在Google Developer Console中註冊的項目ID匹配?

1

變化

using Android.Gms.Gcm.Iid;

using Android.Gms.Iid;

+0

爲我工作:) –

0

有導致這些NotRegistered誤差的Xamarin Studio部署的問題,我的解決方法是詳細here

0

這是一個新的GCM項目嗎?您現在應該使用FCM - Firebase雲消息傳遞。

從GCM網站:

火力地堡雲端通訊(FCM)是GCM的新版本。它繼承了 可靠和可擴展的GCM基礎設施,以及新功能!請參閱 瞭解更多信息。如果您將消息集成到新應用程序中,那麼 應以FCM開頭。強烈建議GCM用戶升級到FCM ,以便從現在和未來的新FCM功能中受益。

我認爲您的GCM密鑰現在可能無效。你應該在這裏創建FCM項目:

https://firebase.google.com/docs/cloud-messaging/