iOS 推送 獲取手機設備的 deviceToken


第一步:申請證書:app




第二步:申請app ids,應用名字必須一致。而後再進入進行編輯,使其enable,綠燈。ide




第三步:申請provisioning profile,生成.mobileprovision,雙擊該證書才能正確導入手機設備,不能拖。spa





第四步:建立應用,使其名字一致。3d



第五步:寫代碼orm


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptionsip

{rem

    // Override point for customization after application launch.string

//    return YES;it

    UIRemoteNotificationType types =io

    (UIRemoteNotificationTypeBadge

     |UIRemoteNotificationTypeSound

     |UIRemoteNotificationTypeAlert);

    

    //註冊消息推送

    [[UIApplication sharedApplication]registerForRemoteNotificationTypes:types];

    return YES;

    

}


//獲取DeviceToken成功

- (void)application:(UIApplication *)application

didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

{

    NSLog(@"DeviceToken: {%@}",deviceToken);

    //這裏進行的操做,是將Device Token發送到服務端

    

    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"DeviceToken:%@",deviceToken] delegate:self cancelButtonTitle:nil otherButtonTitles:@"肯定", nil];

    [alert show];

}


//註冊消息推送失敗

- (void)application:(UIApplication *)application

didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

{

    NSLog(@"Register Remote Notifications error:{%@}",error);

    //    NSLog(@"Register Remote Notifications error:{%@}",error.localizedDescription);

}


//處理收到的消息推送

- (void)application:(UIApplication *)application

didReceiveRemoteNotification:(NSDictionary *)userInfo

{

    NSLog(@"Receive remote notification : %@",userInfo);

    UIAlertView *alert =

    [[UIAlertView alloc] initWithTitle:@"舒適提示"

                               message:@"推送成功!"

                              delegate:nil

                     cancelButtonTitle:@"肯定"

                     otherButtonTitles:nil];

    [alert show];

}

相關文章
相關標籤/搜索