我正在使用推送通知处理一个简单的应用程序,并成功实现了它.当我退出应用程序时,我得到一个推送通知(运行良好),但是当我打开应用程序并尝试从我的服务器(Web应用程序)发送消息时,它不会显示任何弹出消息或通知.我错过了什么吗?这是我在AppDelegate.m上的代码片段
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Let the device know we want to receive push notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
return YES;
}
(void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo{
NSLog(@"Received notification: %@", userInfo);
NSString *messageAlert = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
NSLog(@"Received Push Badge: %@", messageAlert );
[[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object:messageAlert];
}
Run Code Online (Sandbox Code Playgroud)
请帮我解决这个问题.谢谢.
当您的应用程序处于活动模式时,您需要将此方法与下面的方法放在您的Appdelegate类中: -
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
NSString *cancelTitle = @"Close";
NSString *showTitle = @"Show";
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"My App Name"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
[alertView release];
} else {
//Do stuff that you would do if the application was not active
}
}
Run Code Online (Sandbox Code Playgroud)
还要didFailToRegisterForRemoteNotificationsWithError委托检查失败原因
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSString *str = [NSString stringWithFormat: @"Error: %@", error];
NSLog(@"%@", str);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
648 次 |
| 最近记录: |