iOS實現推送淺談

網上好多關於申請推送證書的資料,在這裏就不說申請證書的具體過程了,說一說具體工程項目中應怎麼使用推送,和接收推送消息

首先在AppDelegate的didFinishLaunchingWithOptions方法中中添加如下代碼:

double version = [[UIDevice currentDevice].systemVersion doubleValue];//判定系統版本。
if(version>=8.0f){
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert) categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    }else{
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
    }

然後實現接收推送消息的方法

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ // 處理推送消息
    NSLog(@"userinfo:%@",userInfo);
    NSLog(@"收到推送消息:%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *) error {
    NSLog(@"Registfail%@",error);
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    NSLog(@"%@",deviceToken);//這裏的Token就是我們設備要告訴服務端的Token碼
}

其中,didRegisterForRemoteNotificationsWithDeviceToken方法接收到了token需要發送給服務器,服務器通過這個token值來給相應的設備推送消息。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章