Mannam Brahmaiah
Mannam Brahmaiah

Reputation: 2283

iOS- Push notification issue

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 protected]&regId=4d1d9067cc1382ecb8b0532831cce7fc8eb6fc388a6139060cd84712407a0ae5

Can you please help me out regarding this issue

Upvotes: 0

Views: 217

Answers (2)

Vidhyanand
Vidhyanand

Reputation: 5369

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..!

Upvotes: 1

Allanah Fowler
Allanah Fowler

Reputation: 1281

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.

Upvotes: 0

Related Questions