PushSharp-プッシュ通知の簡単な作業

残念ながら、Habréには長い間トピックリンクがありません。そのため、素晴らしいPushSharpライブラリについて何かを書かなければなりません。

iOS、Android、Windows Phone、およびWindows 8プッシュ通知を送信できます(これは説明にはありませんが、PushSharp.Blackberryフォルダーがソースコードに存在するため、このプラットフォームで既に機能している可能性があります)。 使いやすい場所はありません。

//Create our push services broker var push = new PushBroker(); //Registering the Apple Service and sending an iOS Notification var appleCert = File.ReadAllBytes("ApnsSandboxCert.p12")); //isProduction -    development  ad-hoc/release  push.RegisterAppleService(new ApplePushChannelSettings(isProduction, appleCert, "pwd")); push.QueueNotification(new AppleNotification() .ForDeviceToken("DEVICE TOKEN HERE") .WithAlert("Hello World!") .WithBadge(7) .WithSound("sound.caf")); //Registering the GCM Service and sending an Android Notification push.RegisterGcmService(new GcmPushChannelSettings("theauthorizationtokenhere")); //Fluent construction of an Android GCM Notification //IMPORTANT: For Android you MUST use your own RegistrationId here that gets generated within your Android app itself! push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("DEVICE REGISTRATION ID HERE") .WithJson("{\"alert\":\"Hello World!\",\"badge\":7,\"sound\":\"sound.caf\"}")); 


また、著者はドキュメンテーションサンプルで作業するのが面倒ではありませんでした。 そこからiOS向けのプッシュ通知を設定するための段階的な手順は素晴らしいです。

もちろん、このライブラリにはnugetパッケージがあります。

そして、iOS用に開発するときにデバイストークンを取得する方法を自分から追加します。 プッシュプロビジョニングプロファイルをインストールした後、AppDelegateにメソッドを追加する必要があります。

 - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { //  deviceToken,         } 


デバイストークンを保存するには、NSUserDefaultsを使用します(このアプローチにはまだマイナスは表示されません)。 考慮すべき唯一のことは、トークンがNSDataの形式で上記のメソッドに到達することです(行にキャストすると、「<asdfasdf asdfasdf 12341234 wertwert 1234asdf ewrt3456 asdfasfd asdfasdf>」のようになります)。 asdfasdfasdfasdf12341234wertwert1234asdfewrt3456asdfasfdasdfasdf ")。

つまり、トークンを保存および受信する方法は次のようになります。

 +(NSString*)getUserDeviceToken{ return [[NSUserDefaults standardUserDefaults] stringForKey:_deviceTokenKey]; } +(void)setUserDeviceToken:(NSData*)tokenData{ NSCharacterSet *charactersToRemove = [NSCharacterSet characterSetWithCharactersInString:@"<> "]; NSString *result = [[[NSString stringWithFormat:@"%@",tokenData] componentsSeparatedByCharactersInSet:charactersToRemove] componentsJoinedByString: @""]; [[NSUserDefaults standardUserDefaults] setValue:result forKey:_deviceTokenKey]; } 


また、証明書のインストールに加えて、AppDelegateでプッシュ通知用のアプリケーションを登録する必要があります。

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)]; return YES; } 

Source: https://habr.com/ru/post/J178775/


All Articles