IOS 蘋果官方推送

weixin_34236497發表於2017-08-19

客戶端:

1、告訴應用程式,接收push來的訊息(當然是放在didFinishLaunchingWithOptions方法裡面了)

[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];

2、完成推送比不可缺的東西:deviceToken,蘋果推送會根據deviceToken的值進行推送的操作。deviceToken和全球之內的蘋果裝置一一對應的,也就是說它是唯一的。

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

NSLog(@"獲取裝置的deviceToken: %@", deviceToken);

}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error{

NSLog(@"Failed to get token, error: %@", error);

3、對推送過來的訊息進行處理的方法:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{

//以警告框的方式來顯示推送訊息

if([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]!=NULL) {

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"經過推送傳送過來的訊息"

message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]

delegate:self

cancelButtonTitle:@"關閉"

otherButtonTitles:@"處理",nil];

[alert show];

[alert release];

}

}



#pragma mark是否接收推送訊息

- (BOOL)isAllowedNotification {

if([[UIDevicecurrentDevice].systemVersionfloatValue] >=8.0) {

UIUserNotificationSettings*setting = [[UIApplicationsharedApplication]currentUserNotificationSettings];

if(UIUserNotificationTypeNone!= setting.types) {

returnYES;

}

}else{

UIRemoteNotificationTypetype = [[UIApplicationsharedApplication]enabledRemoteNotificationTypes];

if(UIRemoteNotificationTypeNone!= type)

returnYES;

}

returnNO;

}

相關文章