Urban Airship iOS Push Notifications Not Received By iOS Device - ios

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];

Related

Urban Airship SDK Integration in iOS & push notification

We have integrated Urban SDK using these guidelines.
We have tried to check the push notification using ad hoc profile where we have set below values in AirshipConfig.plist
inProduction=YES
productionAppKey=OUR PRODUCTION KEY
productionAppSecret= OUR PRODUCTION SECRET
Please check the below code which we have implemented in the AppDelegate file of project.
-(void) applicationDidFinishLaunching: (UIApplication *)application
{
.
.
.
.
UAConfig *config = [UAConfig defaultConfig];
config.automaticSetupEnabled=NO;
[UAirship takeOff:config];
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)])
{
NSLog(#"------------REGISTER DEVICE------------: >= 8");
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
}
else
{
NSLog(#"------------REGISTER DEVICE------------: <8 ");
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
.
.
.
.
}
#pragma mark Remote Notification methods
-(void) application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken // Device Registration
{
[[UAirship push] appRegisteredForRemoteNotificationsWithDeviceToken:devToken];
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[[UAirship push] appRegisteredUserNotificationSettings];
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(#"%#", error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"NOTIFICATION------------ didReceiveRemoteNotification ------------ %#", userInfo);
[[UAirship push] appReceivedRemoteNotification:userInfo applicationState:application.applicationState];
}
When we are trying to send the notification to the registered device token, the server shows that device token as INACTIVE Device token.
Please show me what should I do for this.
So, first it looks like you are missing a few app delegate methods.
Urban Airship handles APNS registration. Remove your manual registration calls as UA will just override them, and instead, enable notifications with [UAirship push].userPushNotificationsEnabled = YES;. It should prompt the user to accept notifications, and then the Urban Airship channel should be opted in.

iPhone 6 and 6 Plus Can Not Receive Remote Push Notifications

I have a really strange problem with my app on iPhone 6 and 6 Plus (iOS 8.2). I've followed and read almost everything (I Think!) that I found on the web to figure this out, but still it doesn't work. I've tested the app on iPhone 4 (iOS 7.1), 4s (iOS 7.1), 5s (iOS 8.1) and iPad Mini (iOS 8.2) and they all can receive the push notifications on from the app. My XCode version is 6.2 and the iPhone 6 devices (iOS 8.2) are detected as ineligible devices on it. So I can't even run the app right from XCode.
I'm using php script to push the notifications. When the app first run on iPhone 6, it showed up a pop up alert asking for the push notification permission. On the device's settings, the app is there on the notifications settings. I've tried revoking the apns certificate and creating a new one, re-generating the .pem files, reseting the device contents and settings, uninstall and install the app. None of these ever worked and I am already pulling my hair out. I even asked the Apple technical support for this but their answer is really just blah. I've tried downloading an app from the app store that has push notifications feature. And somewhat the device can receive push notifications from that app. So, is there anything that I am missing here? Can someone help me in this?
As suggested, here's my code for registering the notifications. I called the method "registerForRemoteNotification" from inside "application:didFinishLaunchingWithOptions:" :
- (void)registerForRemoteNotification
{
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)])
{
NSLog(#"system version >= 8.0");
UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert;
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
}
else {
NSLog(#"system version < 8.0");
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
}
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
NSLog(#"Registering for remote notification");
[application registerForRemoteNotifications];
}
#endif
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(#"did register remote notification with device token");
NSString *newToken = [deviceToken description];
newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
newToken = [newToken stringByReplacingOccurrencesOfString:#" " withString:#""];
NSLog(#"Device token: %#",newToken);
DataManager *sharedData = [DataManager sharedInstance];
sharedData.deviceToken = newToken;
[self requestProfileWithDeviceToken:newToken];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(#"Failed to get device token, error: %#", error);
[self requestProfileWithDeviceToken:#""];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"Receive push notification: %#",userInfo);
[self processPushNotification:userInfo];
}
Any help would be very much appreciated. Cheers.

Sinch delegate method "shouldSendPushNotification" not being called

I'm playing with Sinch and have some problems with Push notifications.
Firstly I can use Sinch to send and receive messages (two devices
with two different Sinch IDs). So that means the Sinch Client is
correctly configured.
Secondly I can confirm the push notification are correctly set on
both of the devices, because I can send push notifications to them on
Parse.com. They all have valid push notification tokens.
Then in my app, I found that the Sinch delegate method: shouldSendPushNotification is not called when the receiver side is not "online".
I did a search on SO and found there's a similar question (Sinch, message shouldSendPushNotification not being called) which suggested to check the messageSent call back.
Therefore I tried the following in the receiver side:
put the app into background by pressing home
force quit the app (double click home, remove the app from background)
enable flight mode
After that when a message sent, I can see:
- (void)messageSent:(id<SINMessage>)message recipientId:(NSString *)recipientId
is being invoked in the sender's side and the recipientId is the same as the destination device's. But the shouldSendPushNotification method is never being called as stated in Sinch's documentation.
Since this shouldSendPushNotification method is not invoked, there will not be any push notifications being sent out to the destination device.
I've been working on this problem for several days, and very keen to know the solution, any help is appreciated.
Test environment
two devices in iOS 8 beta 4 and one in iOS 7.1.2
build using XCode 6 beta 4
Code:
Setting up the Sinch messaging service in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
// setup Parse
[Parse setApplicationId:#"xxxxx"
clientKey:#"xxxxx"];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
// use registerUserNotificationSettings
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories: UIUserNotificationActionContextDefault]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
// use registerForRemoteNotifications
// Let the device know we want to receive push notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
#else
// use registerForRemoteNotifications
// Let the device know we want to receive push notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#endif
// app response from the notifications while in background
NSDictionary* remotePush = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remotePush) {
// Extract the Sinch-specific payload from the Apple Remote Push Notification
NSString* payload = [remotePush objectForKey:#"SIN"];
// Get previously initiated Sinch client
id<SINNotificationResult> result = [_client relayRemotePushNotificationPayload:payload];
if (result.isMessage) {
// Present alert notifying
NSString *messageId = [[result messageResult] messageId];
NSLog(#"Received messageid: %#", messageId);
} else if (!result.isValid) {
// Handle error
}
NSLog(#"Received Payload: %#", payload);
}
return YES;
}
- (void)initSinchClientWithUserId:(NSString *)userId {
if (!_client) {
_client = [Sinch clientWithApplicationKey:#"xxxx"
applicationSecret:#"xxxx"
environmentHost:#"sandbox.sinch.com"
userId:userId];
_client.delegate = self;
[_client setSupportMessaging:YES];
[_client setSupportPushNotifications:YES];
[_client setSupportActiveConnectionInBackground:NO];
[_client start];
[_client startListeningOnActiveConnection];
}
}
And this line is called as expected when the app starts
- (void)clientDidStart:(id<SINClient>)client {
NSLog(#"Sinch client started successfully (version: %#)", [Sinch version]);
}
Inside the app's MessageSendingViewController
- (id<SINClient>)client {
return [(AppDelegate *)[[UIApplication sharedApplication] delegate] client];
}
-(void)viewDidLoad {
...
[self.client messageClient].delegate = self;
...
}
Are you registering "push data" (e.g. your APN token) via the method -[SINClient registerPushNotificationData:]?
Try something like:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[_client registerPushNotificationData:deviceToken];
}
(There is also more details here on http://www.sinch.com/docs/ios/user-guide/#pushnotifications)
Problem solved with some help from the Sinch.
First make sure that client is not nil (0x0) when the delegate method registerPushNotificationData:deviceToken is being called.
In my case, I need to manually register notification settings again after starting the Sinch client.
Once the client is started and notification settings are registered, the shouldSendPushNotification method should be called without any problems.

Remote Notifications in "Not running" State

i implemented the Remote Notifications in my application! if my App is in Background and a Push Message was send to my Device, i react with this method:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
...Do Stuff
}
This is working great when App is in Foreground or in Background State! But what if my App is not running at all?! CanĀ“t i react to Push Messages when the app is not running?I mean WhatsApp can do this, right?!
If user clicks on push notification from notification center you will have information in launchOptions with the push notification content and you can use below code to check if application was launched clicking push notification or it was there as well,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
NSLog(#"LaunchOptions->%#",launchOptions);
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
[self performNotificationAction:userInfo];
}
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
// NSLog(#"userInfo->%#",userInfo);
[self performNotificationAction:userInfo];
}
-(void)performNotificationAction:(NSDictionary*)userInfo{
//Do the stuf whatever you want.
//i.e. fetch the message or whatever extra information sent in push notification
}

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