上一篇[iOS Xcode]APNs_Apple Push Notification server(憑證製作)教大家如何申請APNs所需的憑證
接下來要開始教大家寫手機端的程式
這邊很簡單的就是讓大家去向APNs註冊,然後得到一組token
AppDelegate.m
先在application didFinishLaunchingWithOptions中設定APP可以接受哪些下訊息(例如:文字,紅色小圈圈,聲音)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound |
UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
return YES;
}
然後在 didRegisterUserNotificationSettings向APNS註冊
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
傳回向APNS註冊的token,並顯示在NSLog上
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSLog(@"Token : %@",deviceToken);
}
最後只要有看到token出現就算是成功了!
留言列表