iOS-推送通知问题

时间:2016-02-05 04:28:13

标签: ios objective-c

目前我的一位php开发人员为我提供了iOS设备的推送通知API

问题是:如果我在任何浏览器(Chrome / Safari / Firefox等等)中使用相应参数运行该api,我会收到有关iOS设备前景的通知。但不是在iOS应用程序(Xcode)本身

在我的应用中,我使用的代码如下:

- (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);
}

我收到回复:(在didReceiveRemoteNotification中)

{
aps =     {
    alert = "You're login successfully";
    sound = default;
};

}

此消息未显示在状态栏上(屏幕顶部)。在iOS方面(或)PHP方面是否存在任何问题

如果问题出在iOS端 - &gt;我该怎么做

以下是我的测试推送通知API:

https://shopgt.com/mobile1/iphone/register.php?device_type=2&email=sukhpal@anaad.net&regId=4d1d9067cc1382ecb8b0532831cce7fc8eb6fc388a6139060cd84712407a0ae5

你能否帮我解决这个问题

2 个答案:

答案 0 :(得分:1)

您需要在前景中的应用程序中自定义视图以显示推送通知的横幅。您可以使用JCNotificationBannerPresenter。请使用以下链接关注示例代码。

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!");
   }];
}

希望它能帮助你......!

答案 1 :(得分:0)

没有问题,这是默认行为。

当您在主屏幕上时,当您进入应用程序时,不会显示屏幕顶部显示的横幅。

您需要让应用在didReceiveRemoteNotification内自行完成通知。这可能会显示警告,向标签栏添加徽章等,但只有当您在应用程序之外时才会显示横幅。