iOS 推送 獲取手機設備的 deviceToken – iPhone手機開發技術文章 iPhone軟體開發教學課程

第一步:申請證書:

第二步:申請app ids,應用名字必須一致。然後再進入進行編輯,使其enable,綠燈。vcD4KPHA+PGJyPgo8L3A+CjxwPjxpbWcgc3JjPQ==”https://www.aiwalls.com/uploadfile/Collfiles/20140617/2014061708371888.jpg” alt=”\”>

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

第四步:創建應用,使其名字一致。

第五步:寫代碼

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions

{

// Override point for customization after application launch.

// return YES;

UIRemoteNotificationType types =

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

}

You May Also Like