Implementing Pusher APNs Notification - ios

following this promising tutorial from nickjf89 I try to implement Push Notification in a vanilla Cordova project.
Until now, I'm able to get communication with the socket, when I push data from the Pusher console, all works, so I exclude any misconfiguration on Pusher API.
The "only" thing which fail is the actual Push Notification.
Looking at the Push Notification console, I see my request arriving on my cordova channel.
But in the xCode console, I don't see the expected log from NSLog(#"Received remote notification: %#", userInfo);
I suspect I got an issue with my AppDelegate.m which is below.
#import "AppDelegate.h"
#import "MainViewController.h"
#import UserNotifications;
#import <PusherSwift/PusherSwift-Swift.h>
#interface AppDelegate ()
#property (nonatomic, retain, readwrite) Pusher *pusher;
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
self.viewController = [[MainViewController alloc] init];
self.pusher = [[Pusher alloc] initWithKey:#"here_i_put_my_pusher_app_key"];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorisation.
}];
[application registerForRemoteNotifications];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(#"Registered for remote notifications; received device token");
[[[self pusher] nativePusher] registerWithDeviceToken:deviceToken];
[[[self pusher] nativePusher] subscribeWithInterestName:#"cordova"];
NSLog(#"Seeems token stuff works");
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"Received remote notification: %#", userInfo);
}
#end

Ok, one time again, the issue was between the chair and the keyboard...
I found the fix, it was related to the APNs certificate uploaded on Pusher and the my-app-name.entitlements where APS environment was set on production instead of development.
All fixed and functional, great new feature from Pusher!

Related

Google Cloud Messaging in iOS

Hi i want to implement GCM in iOS and i have my SenderID but i dont know where i have to implement SenderID.This is my entire code for GCM integration.i need TOKEN value.so please guide me to get it done.
Appdelegate :
#import "AppDelegate.h"
#interface AppDelegate ()
{
NSDictionary *RegistrationOptions;
NSString *GcmSenderID;
GGLInstanceIDTokenHandler registrationHandler;
}
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIUserNotificationType allNotificationTypes=(UIUserNotificationTypeSound|UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings=[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication]registerUserNotificationSettings:settings];
[[UIApplication sharedApplication]registerForRemoteNotifications];
return YES;
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
// Create a config and set a delegate that implements the GGLInstaceIDDelegate protocol.
GGLInstanceIDConfig *instanceIDConfig = [GGLInstanceIDConfig defaultConfig];
instanceIDConfig.delegate = self;
// Start the GGLInstanceID shared instance with the that config and request a registration
// token to enable reception of notifications
[[GGLInstanceID sharedInstance] startWithConfig:instanceIDConfig];
RegistrationOptions = #{kGGLInstanceIDRegisterAPNSOption:deviceToken,
kGGLInstanceIDAPNSServerTypeSandboxOption:#YES};
[[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:GcmSenderID
scope:kGGLInstanceIDScopeGCM
options:RegistrationOptions
handler:registrationHandler];
}
- (void)onTokenRefresh {
// A rotation of the registration tokens is happening, so the app needs to request a new token.
NSLog(#"The GCM registration token needs to be changed.");
[[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:GcmSenderID
scope:kGGLInstanceIDScopeGCM
options:RegistrationOptions
handler:registrationHandler];
}
Its pretty late answer.But it will help someone in future.
You just have to upload apns certificate (developer and production) and get the gcm config file and add it to your project (drag and drop to your project).
The following code which you should call in the didFinishLaunchingWithOptions will automatically fetch the _gcmSenderID from the config file.
_gcmSenderID = [[[GGLContext sharedInstance] configuration] gcmSenderID];
Here is a sample quick start project for you to start with.(both swift and objective-C).Everything is well explained in this sample app.Let me know if you have any doubts.

PhoneGap push notification for ios using admin.pushapps.mobi

How to receive the PhoneGap push notification in ios platform...?
I am using http://admin.pushapps.mobi console for sending a notification , I have configured everything for ios, and used the app token in my javascript file. I had sent a notification from the admin.pushapps.mobi, but it's not reviewing in my app.
I am using the demo given by them..... below is the full link https://github.com/PushAppsService/PhonegapBuildExampleApp
Can anyone please explain where I am wrong ? if there is any other push notification service having PhoneGap documentation, it would be helpful.
You should check 2 things for PushApps to work as expected:
Certificates and provisioning profile matching.
You should verify the your AppDelegate.m contains as mentioned in the wiki:
#import "PushApps.h"
#pragma mark - Push Notifications
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[[PushAppsManager sharedInstance] didRegisterUserNotificationSettings:notificationSettings];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
[[PushAppsManager sharedInstance] handleActionWithIdentifier:identifier forRemoteNotification:userInfo
completionHandler:completionHandler];
}
#endif
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
// Notify PushApps of a successful registration.
[[PushAppsManager sharedInstance] updatePushToken:deviceToken];
}
// Gets called when a remote notification is received while app is in the foreground.
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo
{
[[PushAppsManager sharedInstance] handlePushMessageOnForeground:userInfo];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
// keeps you up to date with any errors during push setup.
[[PushAppsManager sharedInstance] updatePushError:error];
}
Don't use pushapps.mobi give you all limited notification and user plan and after you get more than on milion users then close your account and then tell you if you want return your account more and more money juswsh layer

Where do I need to receiving Remote push messages in the app

I check for new message in AppDelegate class in 3 places, in didFinishLaunchingWithOptions:(NSDictionary *)launchOptions using code:
if (launchOptions != nil)
{
NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
//NSLog(#"Launched from push notification: %#", dictionary);
//notification pending ... so pull from server the new message
[self addMessageFromRemoteNotification:dictionary updateUI:YES];
}
}
and in didReceiveRemoteNotification:(NSDictionary*)userInfo using code:
// notification pending ... so pull from server the new message
[self addMessageFromRemoteNotification:userInfo updateUI:YES];
and in - (void)applicationDidBecomeActive:(UIApplication *)application using code
if(application.applicationIconBadgeNumber>0)
{
application.applicationIconBadgeNumber = 0;
// notification pending ... so pull from server the new message
[self openMessageViewForNewMessage];
}
However, still I notice there is some cases where my app still dont "catch" notification, meaning, it still not aware that it receive notification. Did I message something? or I should all the time check "my server" for new messages becouse app might not all the times be informed by iOS that there is new notification.
Quick overview...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Here you specify what kind of notifications you want your app to receive. For Example, if you want badge, sound and alert you would include this:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
In the:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
You can add something like this to make sure to update your badge:
NSString *badge = [apsInfo objectForKey:#"badge"];
application.applicationIconBadgeNumber = [badge intValue];
I personally also add this code and do my processing where appropriate:
[[NSNotificationCenter defaultCenter] postNotificationName:#"ReceivedNotificationAlert" object:self];
The above works well for all my apps. You mentioned there are some cases when your app misses APNs. Can you share exactly what kind of cases?

Urban Airship iOS Push Notifications Not Received By iOS Device

I have tried to set up Urban Airship to deliver push notifications to my iOS application with no luck.
Here are the things I have:
Developer Provisioning profile with push notifications enabled
Push Notification Certificate on device and uploaded to Urban Airship
No errors anywhere - UA's error console is empty and I checked that my Device token is active
Here is some snippets from my AppDelegate.m file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Push notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)];
//Init Airship launch options
NSMutableDictionary *takeOffOptions = [[NSMutableDictionary alloc] init];
[takeOffOptions setValue:launchOptions forKey:UAirshipTakeOffOptionsLaunchOptionsKey];
// Create Airship singleton that's used to talk to Urban Airship servers.
// Please populate AirshipConfig.plist with your info from http://go.urbanairship.com
[UAirship takeOff:takeOffOptions];
// Register for notifications
[[UAPush shared] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
[UAirship setLogging:YES];
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
// Updates the device token and registers the token with UA
NSLog(#"APN device token: %#", devToken);
[[UAPush shared] registerDeviceToken:devToken];
}
None of the following methods are ever called when I send a notification through UA's "Test Push Notification" tab, or send a CURL command through terminal
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(#"Error in registration. Error: %#", err.description);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"Received push notification with userInfo:%#", userInfo);
}
- (void)handleNotification:(NSDictionary *)notification applicationState:(UIApplicationState)state
{
NSLog(#"Received push notification with notification:%#", notification);
}
I have tried sending test push notifications with the app closed and iOS does not do anything either. I checked in Settings on the iphone and went to my app and it shows push is enabled for badges and banners. I'm running iOS 6.1 on an iPhone 5.
I figured it out - I must have missed this line from the documentation:
[[UAPush shared] handleNotification:[launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]
applicationState:application.applicationState];

Device Token incorrect with Urban Airship?

I'm trying to implement push notifications in my app. So, for testing, I built up a new app and did all the necessary steps (I hope...). I added to my project the following:
-AirshipConfig.plist
-libUAirship-1.1.4.a
-UAGLobal.
-UAirship.h
-UAObservable.h
-UAPush.h
and all the frameworks listed on their website.
My AppDelegate.m is:
#import "AppDelegate.h"
#import "UAirship.h"
#import "UAPush.h"
#implementation AppDelegate
#synthesize window = _window;
-(void)setupPushWithOptions:(NSDictionary *)launchOptions {
//URBAN AIRSHIP PUSH NOTIFICATION CONFIGURATION
//Init Airship launch options
NSMutableDictionary *airshipConfigOptions = [[NSMutableDictionary alloc] init];
//[airshipConfigOptions setValue:#"5QQmJyTMRZWks0nbx-9pHQ" forKey:#"DEVELOPMENT_APP_KEY"];
//[airshipConfigOptions setValue:#"OMuzzHdCQOCnrOtfiWox9Q"
forKey:#"DEVELOPMENT_APP_SECRET"];
[airshipConfigOptions setValue:#"xrUoy0B1RdyjZqZXEuwIsg" forKey:#"PRODUCTION_APP_KEY"];
[airshipConfigOptions setValue:#"qiRlUvoaSHGNeXxw9pj71w" forKey:#"PRODUCTION_APP_SECRET"];
#ifdef DEBUG
[airshipConfigOptions setValue:#"NO" forKey:#"APP_STORE_OR_AD_HOC_BUILD"];
#else
[airshipConfigOptions setValue:#"YES" forKey:#"APP_STORE_OR_AD_HOC_BUILD"];
#endif
NSMutableDictionary *takeOffOptions = [[NSMutableDictionary alloc] init];
[takeOffOptions setValue:launchOptions forKey:UAirshipTakeOffOptionsLaunchOptionsKey];
[takeOffOptions setValue:airshipConfigOptions
forKey:UAirshipTakeOffOptionsAirshipConfigKey];
// Create Airship singleton that's used to talk to Urban Airship servers.
// Please replace these with your info from http://go.urbanairship.com
[UAirship takeOff:takeOffOptions];
[[UAPush shared] resetBadge];//zero badge on startup
[[UAPush shared]
registerForRemoteNotificationTypes:UIRemoteNotificationTypeNewsstandContentAvailability|UIRemoteNotificationTypeAlert]; // register for Newsstand and Alerts
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
//Init Airship launch options
NSMutableDictionary *takeOffOptions = [[NSMutableDictionary alloc] init];
[takeOffOptions setValue:launchOptions forKey:UAirshipTakeOffOptionsLaunchOptionsKey];
// Create Airship singleton that's used to talk to Urban Airship servers.
// Please populate AirshipConfig.plist with your info from http://go.urbanairship.com
[UAirship takeOff:takeOffOptions];
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
// Override point for customization after application launch.
[self setupPushWithOptions:launchOptions];
return YES;
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
UALOG(#"APN device token: %#", deviceToken);
NSLog(#"%#", deviceToken);
// Updates the device token and registers the token with UA
[[UAirship shared] registerDeviceToken:deviceToken];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
-(void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(#"Failing in APNS registration: %#",error);
}
#end
In this way I get the Error Apple Push service rejected device token
But I also figured out that my device token could be retrieved not correctly. Following a UA member staff suggestion I downloaded from the Appstore the App Wide Angle, and I launched it witht my iPhone connected and with the Xcode console open. So I can see that my device token is another one...Where could it be the problem?
You shouldn't hardcode your variabels into Urban Airship like this.
Create AirshipConfig.plist The library uses a .plist configuration
file named AirshipConfig.plist to manage your production and
development application profiles.
Create 2 applications within your Urban Airship account - one for
development & another for production. Ex. Name_of_your_app_dev
Name_of_your_app_prod Create an AirshipConfig.plist file Set the
following values to the ones in your applications

Resources