Home .NET PushSharp- simple work with Push Notifications

PushSharp- simple work with Push Notifications

by admin

Unfortunately, there are no link-topics on Habra for a long time, so I’ll have to write something about the wonderful library PushSharp
Can send iOS, Android, Windows Phone and Windows 8 Push Notifications (not in the description, but in the source code there is a folder PushSharp.Blackberry, so maybe for this platform already works).It’s really easy to use:

//Create our push services brokervar push = new PushBroker();//Registering the Apple Service and sending an iOS Notificationvar appleCert = File.ReadAllBytes("ApnsSandboxCert.p12"));//isProduction - whether you use the development or ad-hoc/releasepush certificate.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 Notificationpush.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 lazy to work on the documentation and examples Step-by-step instructions for setting up push notifications for iOS from there is just great.
Nuget package this library has, of course, has
I will also add for myself how to get the 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{//there we save the deviceToken and then use it when accessing your server}

I use NSUserDefaults to store device token (I don’t see any disadvantages in this approach yet). The only thing to consider is that the token comes to the above method in the form of NSData (when reduced to a string we get something like "<asdfasdf asdfasdf 12341234 wertwert 1234asdf ewrt3456 asdfasfd asdfasdf> "), and we will need it as a string ("asdfasdfasdfasdf12341234wertwert1234asdfewrt3456asdfasdfasdfasdfasdf").
So the methods to save and retrieve the 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, in addition to installing the certificate, you need to register the push notifications app in your AppDelegate:

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

You may also like