iOS- Push notification issue - ios

Currently one of my php developer provided me push notification API for iOS devices
The problem is : If i run that api with respective parameter in any Browser(Chrome/Safari/Firefox and etc..) i am getting notification on foreground of iOS device. But not in iOS app(Xcode) itself
In my app i used code like:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Register for Push Notitications, if running on iOS 8
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}
#pragma mark
#pragma mark -- Push Notification Delegate Methods
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings: (UIUserNotificationSettings *)notificationSettings{
//register to receive notifications
[application registerForRemoteNotifications];
}
-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{
// Prepare the Device Token for Registration (remove spaces and < >)
devToken = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:#"<"withString:#""]
stringByReplacingOccurrencesOfString:#">" withString:#""]
stringByReplacingOccurrencesOfString: #" " withString: #""];
NSLog(#"My token is: %#", devToken);
// My token is: cd2887c4093569b3eed142320f21a81e521e486cf5c40467390901d3a191398b
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:deviceToken forKey:#"deviceToken"];
}
-(void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error{
NSLog(#"Failed to get token, error: %#", error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(#"%s..userInfo=%#",__FUNCTION__,userInfo);
}
I am getting response:(in didReceiveRemoteNotification)
{
aps = {
alert = "You're login successfully";
sound = default;
};
}
This message is not showing on Status bar(top of the screen). Is there any issue in iOS side (or) PHP side
If the issue is in iOS side--> How can i do this
Here is my Testing Push notification API:
https://shopgt.com/mobile1/iphone/register.php?device_type=2&email=sukhpal#anaad.net&regId=4d1d9067cc1382ecb8b0532831cce7fc8eb6fc388a6139060cd84712407a0ae5
Can you please help me out regarding this issue

You need to customize the view for showing Banner of Push Notification while the app in Foreground. You can use JCNotificationBannerPresenter. Follow the sample code using below link.
https://github.com/jcoleman/JCNotificationBannerPresenter
#import "JCNotificationCenter.h"
#import "JCNotificationBannerPresenterSmokeStyle.h"
- (void) application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)notification {
NSString* title = #"Push Notification";
NSDictionary* aps = [notification objectForKey:#"aps"];
NSString* alert = [aps objectForKey:#"alert"];
[JCNotificationCenter
enqueueNotificationWithTitle:title
message:alert
tapHandler:^{
NSLog(#"Received tap on notification banner!");
}];
}
Hope it Helps you..!

There is no issue, this is default behaviour.
The banner that appears at the top of the screen when you are on the Home screen does not appear when you are inside the app.
You need to get the app to do something with the notification yourself, inside didReceiveRemoteNotification. This could be showing an alert, adding a badge to the tab bar, etc. but banners only show when you are outside of the app.

Related

Push notification permission alert does not appear

In the following code, the device token is gotten.
But Alert View which confirms push notification permission does not appear.
Maybe for that reason, although sending push notification, it does not reach the device.
Do you have any idea why the Alert does not appear?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *deviceTokenString = [deviceToken.description stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:#" " withString:#""];
NSLog(#"DeviceToken: %#", deviceTokenString);
}
Deployment target is iOS8.0.
Push Notifications status in Provisioning Profile is "Enabled".

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.

Receiving APNS notification but cant display on device in ios

Hello everyone,
I am trying how to implement pushnotification.For this i have read apple official document for push notification and also read raywenderlich blog and i understand the flow of pushnotication very well. I have created development and production certificate,profile and its working fine and push was successfully sent and receiving in -
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
*** display message in uialertview******
}
but my problem is how can i display push in my device like other push notification on top up side for both when my application is foreground and background too.
currently i am trying for IOS 7.0 and XCode Version 5.1.1 (5B1008)
Thanks In advance.
First of all check via these methods in App Delegate that if your registered successfully to APNS.
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
}
then in
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSDictionary *Notification = userInfo;
NSString *title = [(NSDictionary*)[(NSDictionary*)[Notification valueForKey:#"aps"] valueForKey:#"alert"] valueForKey:#"title"];
NSString *body = [(NSDictionary*)[(NSDictionary*)[Notification valueForKey:#"aps"] valueForKey:#"alert"] valueForKey:#"body"];
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
{
}
else if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateInactive || [[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground)
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = userInfo;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = body;
localNotification.fireDate = [NSDate date];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
}
If your application is in active state show UIAlertView. if its not you need to show a UILocalNotification.
When you are in Background mode then push notification will display as per your application notification settings from notification centre. you dont have to display ios will do that.
when you are in Foreground mode then notification will receive and -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo method will be called. so using this method you get userinfo. just NSLog userinfo dictionary and then you can create customview with label at top and animate it like ios default banner.
Hope this will help you.
As per Apple's note push/local notification will be display only when app is background mode. If notification is arrive at the time of app is on foreground/active then you need to manually manage it because iOS won't show a notification banner/alert That's default design.
I just put my logic here for manage notification when app in foreground mode:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// check the application state for app is active or not.
if (application.applicationState == UIApplicationStateActive)
{
// Nothing to do if applicationState is Inactive, the iOS already displayed an alert view.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Receive a Remote Notification" message:[NSString stringWithFormat:#"Your App name received this notification while it was running:\n%#",[[userInfo objectForKey:#"aps"] objectForKey:#"alert"]]delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
If you want to receive notification in top of the device like banner style then follow these steps.
First, you need to launch the 'Settings' app on your iOS device.
Once you're in, choose 'Notifications' from the list of options.
Here's a list of every app that supports push notifications. The ones at the top have been granted permission by you
You can also set the alert style. You can choose the banners that conveniently appear at the top of the screen, or full-on pop-ups that force you to take action before they go away. Or you can just choose 'None'.
For more detail check this link.
Hope you will get something from my answer.

How to detect if user tap on "Don't Allow" on Apple's push notification confirmation alert

I want to fire some event if user taps on "Don't Allow" button on the apple's push notification alert message. Is there any notification getting fired or any other way to detect this action from the user?
I'm sure someone will need a solid and simple answer to this (like I once did) -- so here you go. Directly after calling [[UIApplication sharedApplication] registerForRemoteNotifications]; you can use NSNotificationCenter beautifully like so:
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification * _Nonnull note) {
if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) {
//user tapped "Allow"
}
else{
//user tapped "Don't Allow"
}
}];
NOTE: My device is currently running iOS 9.2, my Xcode is version 7.2, and my Deployment Target is 8.0.
I do't thing so that we can detect what UIAlertView button user pressed as there is no any kind of callback methods or delegate etc provided in iOS.
Only if you pressed Don't Allow this will disable the push notification service for that particular iOS App and if YES then enable.
And After that through the code we can check and ensure about it using.
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone)
// NONE
iOS8 comes with rregisterUserNotificationSettings: delegate method. Using this method we can do some patches.Please review them and Let us know your comments.Please these is only work with iOS8.
=> Add/Register Notification.
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)])
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
}
=> Here Below method is called After Alert Notification Fire. Using Any of the Button Action(Don't allow or Allow) we force fully register Notification. and Dow with some patch Here.
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
}
=>We do some trick here
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
NSLog(#"devToken: %#",devToken);
#if !TARGET_IPHONE_SIMULATOR
NSString* deviceToken = [[[[[devToken description]
stringByReplacingOccurrencesOfString: #"<" withString: #""]
stringByReplacingOccurrencesOfString: #">" withString: #""]
stringByReplacingOccurrencesOfString: #" " withString: #""] retain];
NSLog(#"deviceToken : %#",deviceToken);
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
if((![[standardDefaults valueForKey:#"DeviceToken"] isEqualToString:deviceToken]) || [standardDefaults valueForKey:#"DeviceToken"]==nil){
[self sendProviderDeviceToken:deviceToken];
}else{
//Do Some Stuff Here
}
}

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?

Resources