实施iOS推送通知的问题

时间:2014-01-11 16:40:20

标签: ios notifications push-notification apple-push-notifications

我已向我的应用添加了远程通知,但只有在应用处于前台时才有效。当应用程序在后台运行时,我没有任何动作。

我在appDelegate中的代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    // Override point for customization after application launch.
    NSLog(@"Registering for push notifications...");
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

    return YES;
}


-(void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSString*str =[NSString stringWithFormat:@"Device Token=%@",deviceToken];
    NSLog(@"%@",str);

}

-(void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError*)err
{
    NSString*str =[NSString stringWithFormat: @"Error: %@", err];
    NSLog(@"%@",str);
}



- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSDictionary *messageBundleReceived = [userInfo objectForKey:@"CLS-Alert"];
    NSString *messageReceived = [messageBundleReceived objectForKey:@"alert"];

    NSString *badgeNumberReceived = [messageBundleReceived objectForKey:@"badge"];

    int badgeNumber = badgeNumberReceived.intValue;

    UIAlertView *remoteAlert = [[UIAlertView alloc] initWithTitle:@"Attention!!"
                                                          message:messageReceived
                                                         delegate:self cancelButtonTitle:@"Ok"
                                                otherButtonTitles:nil, nil];
    [remoteAlert show];

    [UIApplication sharedApplication].applicationIconBadgeNumber = badgeNumber;
}

我正在使用PushMeBaby来执行推送,并且已经处理了所有设置,例如证书,以及配置设备,因为它确实在前台接收到推送。每个选项也在通知中心中打开。

当应用在后台时,有关为什么不会弹出alertView的任何想法?

1 个答案:

答案 0 :(得分:0)

您可能已经找到了解决方案,但可能对其他人有用。针对iOS7及以上版本的用户可以使用

didReceiveRemoteNotification:fetchCompletionHandler

appdelegate方法,请查看以下链接,说明清楚

didReceiveRemoteNotification not working in the background