我明白了。我沒有意識到Android應用程序需要註冊通知中心;這很愚蠢的回顧。
通常這由所有演練使用的Xamarin組件GoogleCloudMessagingClient
處理,但我不想使用該組件,因爲我正在學習並希望看到樣板代碼。
對於任何人採取這種方法,這裏有我需要添加的東西(這不是複製粘貼代碼使用,只要能表達出我缺少的環節):
var cs = ConnectionString.CreateUsingSharedAccessKeyWithListenAccess(
new Java.Net.URI("sb://YOUR-SERVICE-BUS.servicebus.windows.net/"),
"YOUR-KEY"); // you can get these two values from Azure Console -> Your NotificationHub -> Settings -> Access Policies
var hubName = "your-hub-name"; //the name of your Notification Hub resource on Azure
hub = new NotificationHub(hubName, cs, context); //create the hub (this class is in WindowsAzure.Messaging namespace, which is accessible after you add the Azure Messaging component to your android project)
hub.Register(registrationId, "YOUR_TAG"); //tag can be anything (alphanumeric, -, _, no spaces)
//hub.Register registers this device and its registrationId (from cloud messaging) to the Notifications Hub.
這是除所有其他的事情,你需要做的所有官方演練。我會在這裏列出他們,因爲有人可能會發現它有幫助。步
列表,谷歌詳情
火力地堡:
- 創建新項目+設置推送通知
天青:
- 從火力地堡項目設置
Android客戶端項目服務器密鑰創建通知中心
耗材通知中心:
- 添加Xamarin.GooglePlayServices。GCM通過的NuGet
- 添加Azure的消息組件(右鍵單擊組件在項目資源管理器,單擊 「獲取更多組件」)
- 下列權限添加到
AndroidManifest.xml
:
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<permission android:name="[YOUR PACKAGE NAME HERE].permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="[YOUR PACKAGE NAME HERE].permission.C2D_MESSAGE" />
在MainActivity
中,檢查Google Play服務是否可用。
如果是這樣,創建一個RegistrationIntentService
服務給客戶端設備與火力地堡雲端通訊
啓動此服務在MainActivity
onCreate()
方法註冊
實現一個InstanceIDListenerService
服務(如應用程序registrationID變化,谷歌很容易瞭解詳細信息)
用於接收和響應雲消息(谷歌很容易瞭解詳細信息)
聲明你的三個服務AndroidManifest.xml
,例如GcmListenerService
實現一個GcmListenerService
服務一樣容易對於m如下(谷歌礦石詳細信息):
<application android:label="YOUR_APP_NAME">
<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="[YOUR APP PACKAGE NAME HERE]" />
</intent-filter>
</receiver>
</application>
如果使用Xamarin,你可以簡單地使用,而不是編輯AndroidManifest.xml
的Service
屬性,這將增加所需的報關單清單給你,例如
[Service(Exported = false)]
class RegistrationIntentService : IntentService
{
- 創建
NotificationHub
實例和註冊唯一令牌/ registrationID火力地堡(在文章的開頭,例如我的缺失的環節按代碼)返回的裝置。
在「測試發送」刀片上,在發送測試後,您是否在刀片底部的「結果」部分中看到您的設備註冊?本部分顯示集線器向其發送通知的設備以及通知結果。 此外,「測試發送」僅向最多10個設備發送通知,因此如果您的測試通知以超過10個設備爲目標,則不是所有設備都會收到它。 –
感謝您的回覆。不,我沒有看到註冊@DmitryP。無論發送多少條消息,「測試發送」刀片的「結果」部分都是空的。成功敬酒說:「成功發送測試消息。結果:{0通過,0失敗}。詳情請參閱結果部分。」 我已將它設置爲Android,並且我試圖將「標記」字段留空併發送客戶端的註冊令牌。 當我發送測試消息時,該應用程序處於焦點狀態(儘管我也嘗試過,但它也在後臺)。我已將Firebase API密鑰添加到通知中心。 – TomEverin