More Related Content
Similar to AzureとXamarin.Formsでプッシュ通知 (20)
More from Satoru Fujimori (6)
AzureとXamarin.Formsでプッシュ通知
- 2. 自己紹介
名前:藤森 智(ふじもり さとる)
所属:株式会社ケイ・ジェイ・システムズ
• Xamarin&Azure関係
• 古典データの検索サービス
• DTPや電子書籍などのドキュメント生成をクラウドでおこなうなどの開発支援
• WinForms/PHP/MySQL/C++等のレガシーをXamarin&Azureに随時移行中
趣味:料理
• ひたすら刻む or ひたすら包む
• 「レシピもサンプルコードも三回作れば(書けば)自分のもの」
Twitter:@masatoru
Blog:http://masatoru.hatenadiary.jp/
- 4. プッシュ通知の実装手順(iOSの場合)
https://is.gd/FStVJm
Azureの設定
• 通知ハブを構成する
Xcodeでの設定
• Bundle Identifierの作成
アプリケーションIDの登録
• developer.apple.com/account
証明書要求(CSR)の作成(Mac)
• 作成→クライアントへ登録
SSL証明書の作成
• 作成→ダウンロード→クライアントへ登録
Azureの設定(その2)
• APNSの登録
バックエンドの実装
• 接続文字列、通知ハブの名前・名前空間
• Notification Hubへ接続
• 通知ハブへメッセージを送信
クライアント側の設定
• Visual Studio For Mac
クライアント側の実装
• RegisteredForRemoteNotificationsメソッド
(デバイスを通知ハブへ登録する)
• DidReceiveRemoteNotificationメソッド
(メッセージを受信する)
“たった”これだけ。
- 5. はまりどころ
Xamarin.Formsにプッシュ通知を追加する(Mobile Appsドキュメント)
https://is.gd/F7ygRT
public override void RegisteredForRemoteNotifications(UIApplication application,NSData deviceToken){
const string templateBodyAPNS = "{"aps":{"alert":"$(messageParam)"}}";
JObject templates = new JObject();
templates["genericMessage"] = new JObject {
{"body", templateBodyAPNS}};
// Register for push with your mobile app
Push push = TodoItemManager.DefaultManager.CurrentClient.GetPush();
push.RegisterAsync(deviceToken, templates);
}
→がしかし、動作
しないことがある
MobileServiceClientクラス
…AppServiceのURLのみで通知
ハブ情報は設定しない
- 8. 回避策その2
Xamarinネイティブ(下記はXamarin.iOS)の方法を使う https://is.gd/FStVJm
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) {
// 通知ハブへ接続する
var cs = SBConnectionString.CreateListenAccess(new NSUrl(通知ハブのエンドポイント),
通知ハブのアクセスキー);
var hub = new SBNotificationHub(cs, 通知ハブのハブ名); // 通知ハブにデバイスを登録する
hub.RegisterNativeAsync(deviceToken, null, err => {
//エラー処理
…
通知ハブのエンドポイント App Service→Notification Hub→[アクセスポリ
シー]→[接続文字列]のEndPoint=の部分
通知ハブのアクセスキー Notification Hub→[アクセスポリシー]→[接続文字列]
のSharedAccessKey=の部分
通知ハブのハブ名 Notification Hub→[プロパティ]→[名前
- 9. まとめ
Xamarin.Formsでのプッシュの実装は(現時点では)Mobile App SDKのプッシュ通知
機能は使わない方がよい(感じ)
• 代替え案として、AppServiceの旧方式、またはXamarinネイティブの方法を使う
(参考)
• 最新のAzure Mobile Apps .NET SDK http://bit.ly/2gYuT9X
• aka.ms/zumobook Chapter5 Push Notification https://is.gd/SvlnNO
MSの日本語版だけでなく英語版やXamarinのドキュメントも見よう
• Mobile Apps SDKのドキュメントには FailedToRegisterForRemoteNotifications の記載がない
• Xamarinガイド https://is.gd/cEq4OF
Xamarin.Formsの資料だけでなくXamarin.iOS, Xamarin.Androidの資料も見よう
• Xamarin.Formsだからといってネイティブから逃げない