源码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//当程序处于关闭状态收到推送消息时,点击图标会调用
// 处理推送
if (launchOptions != nil) {
NSDictionary *dictionary = [launchOptionsobjectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil) {
// 这个字典就是推送消息的userInfo
NSLog(@"push 推送的消息---- %@",dictionary);
}
}
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
// iOS 8 Notifications
[application registerUserNotificationSettings:[UIUserNotificationSettingssettingsForTypes:(UIUserNotificationTypeSound |UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)categories:nil]];
[application registerForRemoteNotifications];
}
else
{
// iOS < 8 Notifications
[application registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
return YES;
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSString *token = [[[[NSStringstringWithFormat:@"%@",deviceToken]stringByReplacingOccurrencesOfString:@" "withString:@""]stringByReplacingOccurrencesOfString:@"<"withString:@""]stringByReplacingOccurrencesOfString:@">"withString:@""];
NSLog(@"token---%@",token);
NSUserDefaults *user_defaults = [NSUserDefaultsstandardUserDefaults];
[user_defaultssetObject:token forKey:@"DEVICETOKEN"];
[user_defaultssynchronize];
NSLog(@"My token is: %@", deviceToken);
NSString *devID= [[NSStringalloc]initWithFormat:@"%@", deviceToken];
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}
//处理收到的消息推送
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"Receive remote notification : %@",userInfo);
UIAlertView *alert =
[[UIAlertViewalloc] initWithTitle:@"温馨提示"
message:[[NSStringalloc]initWithFormat:@"%@",userInfo]
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alertshow];
}
转载请注明出处:http://blog.csdn.net/sevenquan
iOS开发社区群:496146057