【发布时间】:2014-01-11 16:40:19
【问题描述】:
我已向我的应用程序添加了远程通知,但它仅在应用程序处于前台时才有效。当应用程序在后台运行并且我进行推送时,没有任何反应。
我在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 的任何想法?
【问题讨论】:
-
当应用程序在前台并且您收到推送通知时会发生什么? (因为我没有看到正在使用任何
alertView代码)。如果您正在谈论推送通知警报类型,请转到设置并将应用程序警报类型更改为您想要的 -
在上面的 didReceiveRemoteNotification 方法中,我显示了一个 AlertView。我希望当应用程序在后台时也能发生这种情况。
-
AlertView 在用户将您的应用置于前台(通过点击通知栏)之前不会出现
-
我没有收到通知横幅。
-
您是否为您的应用指定了后台模式?
标签: ios notifications push-notification apple-push-notifications