iOS消息推送


源码:


- (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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值