2015-02-11 180 views
1

我最近實施了PushSharp到一個應用程序,但對於我的生活我無法弄清楚爲什麼通知沒有出現在設備上。PushSharp GCM消息傳遞

以下是放在一起測試行爲的方法:

private void SendPushNotification() 
{ 
    var push = new PushBroker(); 

    //Wire up the events for all the services that the broker registers 
    push.OnNotificationSent += NotificationSent; 
    push.OnChannelException += ChannelException; 
    push.OnServiceException += ServiceException; 
    push.OnNotificationFailed += NotificationFailed; 
    push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired; 
    push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged; 
    push.OnChannelCreated += ChannelCreated; 
    push.OnChannelDestroyed += ChannelDestroyed; 

    push.RegisterGcmService(new GcmPushChannelSettings("My-API-Key-here")); 

    push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("MyReallyLongRegisteredDeviceKeyHere") 
       .WithJson(@"{""alert"":""Hello World!"",""badge"":7,""sound"":""sound.caf""}")); 


    //Stop and wait for the queues to drains 
    push.StopAllServices(); 
} 

這種方法在應用程序啓動立即打電話,所以我希望我得到通知馬上,我不知道。

還有什麼奇怪的是,事件OnNotificationSent被調用,這將表明消息已經通過。

還有什麼我需要配置爲了這個工作?該文件指出,發件人ID,包名和API密鑰,需要以發送通知

https://github.com/Redth/PushSharp/wiki/How-to-Configure-&-Send-GCM-Google-Cloud-Messaging-Push-Notifications-using-PushSharp#setting-up-pushsharp-to-send-notifications

所有,但API密鑰,雖然使用。

任何幫助將不勝感激。

+0

您是否已將包名更改爲您的包名?例如,BroadcastReceiver。 – ztan 2015-02-11 17:30:02

+0

您是指服務器還是應用程序?如上所述,我看不到任何文件,這將表明我需要更新包名 – 2015-02-11 22:27:35

+0

從這個頁面:https://github.com/Redth/PushSharp'看看PushService.cs文件中示例項目。您可以將大部分此類複製到您自己的應用程序中,但同樣要確保在適用的情況下(您需要更改BroadcastReceiver屬性)替換您自己的軟件包名稱。 ' – ztan 2015-02-11 22:34:00

回答

0

從我的經驗,我面對這個問題,3倍,在過去的兩年裏:)

第一次:我不得不重新生成新的GCM API密鑰,用它和它的工作。

第二次:第一個解決方案沒有工作,我與android開發人員仔細檢查了他的代碼,結果發現他的代碼有問題。

第三次:問題出在設備上,我不得不重新啓動設備,之後馬上收到通知。

+0

我嘗試了你建議的3種解決方案,但沒有任何效果。你有其他解決方案嗎?我在 – 2015-08-19 12:11:04

+0

這個問題上面對完全相同的問題我不確定你的情況是否與這一個完全一樣,也許如果你用你使用的代碼在新線程上發佈你的問題將會是最好的 – 2015-08-19 12:24:44

+0

我的問題有已經解決了,就像你提到的第二種情況。問題出在Android應用程序中 – 2015-08-20 05:46:45

0

嘗試使用消息而不是提醒。

.WithJson(@"{""message"":""Hello World!"",""badge"":7,""sound"":""sound.caf""}")); 

就像我的情況一樣,服務(就像您的情況一樣)發送警報,但客戶端設備正在等待消息。

問候,

0

對於PushSharp 4.0,你可以去這樣

var config = new GcmConfiguration("senderKey", "apiKey", null); 
config.GcmUrl = "https://fcm.googleapis.com/fcm/send"; //!!!!!gmc changed to fcm;) 

var gcmBroker = new GcmServiceBroker(config); 
gcmBroker.Start(); 

_gcmBroker.QueueNotification(new GcmNotification 
       { 
        RegistrationIds = new List<string> { "YourDeviceToken" }, 
        Notification = JObject.Parse(
         "{" + 
          "\"title\" : \"" + yourMessageTitle + "\"," + 
          "\"body\" : \"" + yourMessageBody + "\"," + 
          "\"sound\" : \"mySound.caf\"" + 
         "}") , 
        Data = JObject.Parse(
         "{" +        
          "\"CustomDataKey1\" : \"" + yourCustomDataValue1 + "\"," + 
          "\"CustomDataKey2\" : \"" + yourCustomDataValue2 + "\"" + 
         "}") 
       }); 

如果自定義數據值到達,但該通知確實:)的項目我沒有測試用「你」開頭是你的動態參數,如你傳遞給該方法的參數等等。很久以前問過的問題,但我剛開始使用pushsharp :)希望這有助於PushSharp用戶。