📜 ⬆️ ⬇️

PushSharp - Simple Push Notifications

Unfortunately, there are no topic links for Habré for a long time, so you have to write something about the wonderful PushSharp library.

It can send iOS, Android, Windows Phone and Windows 8 Push Notifications (it’s not in the description, but the PushSharp.Blackberry folder is in the source code, so maybe it already works for this platform) It's easier to use it at all:

//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\"}")); 

')
Also, the authors were not too lazy to work on the documentation and examples . The step-by-step instruction for setting push notifications for iOS from there is simply wonderful.

Nuget package, of course, has this library.

And add on my own how to get device token when developing for iOS. After installing the Push provision profile, you will need to add a method to your AppDelegate:

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


To save the device token, I use NSUserDefaults (I don’t see any minuses in this approach yet). The only thing that needs to be considered is token comes in the above method as NSData (when casting to a string, we get something like "<asdfasdf asdfasdf 12341234 wertwert 1234asdf ewrt3456 asdfasfd asdfasdf>"), and we will need it as a string (" asdfasdfasdfasdf12341234wertwert1234asdfewrt3456asdfasfdasdfasdf ”).

That is, the methods for saving and retrieving a token will look like this:

 +(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]; } 


Also, besides installing the certificate, you need to register the application for push notifications in your AppDelegate:

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

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


All Articles