蘋果原生推送,蘋果
很久以前寫demo,現在回憶下~~~可能有點錯誤
GIthub:https://github.com/BigShow1949/YFPushTest
步驟:
1)添加APP ID.
蘋果開發人員中心: Identifiers--->App IDs 添加一個ID,比如我的Name:YFPushTest, 這裡的ID一定是項目的Bundle Identifier , 比如:com.YFPushTest.BigShow.YFPushTest;
2)產生蘋果推送認證(生產&測試)
取名比如: com.YFPushTest.BigShow.YFPushTest, 選擇APP ID的時候,要選擇我們剛才建立的那個,不要選其他了的(很多AppID時候,容易忘記)
3)產生描述檔案
取名:YFPushTestProfile, 同樣, AppID不要選錯了
4)上代碼:
1 #import "AppDelegate.h" 2 3 @interface AppDelegate () 4 5 @end 6 7 @implementation AppDelegate 8 9 10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 11 { 12 // application.applicationIconBadgeNumber = 0; 13 14 if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) 15 { 16 // 1.請求授權可以給使用者發送通知 17 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert categories:nil]; 18 19 [application registerUserNotificationSettings:settings]; 20 } 21 else 22 { 23 [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound]; 24 } 25 26 if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) 27 { 28 // 跳轉 29 } 30 31 return YES; 32 } 33 34 35 /** 36 * 遠程推送註冊成功 37 * 38 * @param deviceToken deviceToken 39 */ 40 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 41 { 42 // 25bb75ac 3ffcebd7 90d9f517 1ebca904 154a367a 87781e5d b9ea288e 37fdf487 43 NSLog(@"-----deviceToken ---- ---- %@ devDesc ---%@", deviceToken,deviceToken.description); 44 45 //tokenStr 得到可用的token。 46 NSString *tokenStr = [NSString stringWithFormat:@"%@",deviceToken]; 47 tokenStr = [tokenStr stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];//將其中的<>去掉 48 tokenStr = [tokenStr stringByReplacingOccurrencesOfString:@" " withString:@""];//將其中的空格去掉 49 50 NSLog(@"token--- %@",tokenStr); 51 // //註冊成功,返回token 52 // UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"APNS返回的Token:" message:tokenStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil]; 53 // 54 // [alert show]; 55 56 // applicationIconBadgeNumber 57 } 58 59 60 /** 61 * 註冊失敗 62 * 63 */ 64 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 65 { 66 67 NSLog(@"註冊失敗 ---- %@",error); 68 NSString *tokenStr = [NSString stringWithFormat:@"%@",error]; 69 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"註冊失敗!" message:tokenStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil]; 70 [alert show]; 71 NSLog(@"註冊失敗%@",error); 72 73 74 }//NS_AVAILABLE_IOS(3_0); 75 76 77 78 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 79 { 80 NSLog(@"接收到推播通知 ---- %@", userInfo); 81 82 /* 83 84 "name" : "YFPushTest", 85 "action_type" : "1", 86 "message" : "您的車輛京KKKKKK於2016-4-22 駛入邯鄲", 87 "aps" : { 88 "alert" : "This is some fancy message.", 89 "badge" : 1, 90 "sound" : "您的車輛京KKKKKK於2016-4-22 駛入邯鄲" 91 }; 92 93 */ 94 95 // application.applicationIconBadgeNumber -=1; 96 //將推送訊息以alert形式呈現 97 NSString *message = [[userInfo objectForKey:@"aps"]objectForKey:@"alert"]; 98 99 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];100 101 [alert show];102 }103 104 105 // This callback will be made upon calling -[UIApplication registerUserNotificationSettings:]. The settings the user has granted to the application will be passed in as the second argument.106 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings107 {108 109 NSLog(@"ls---- %@",notificationSettings);110 111 // 2.註冊遠程通知112 [application registerForRemoteNotifications];113 114 }//NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;115 116 117 // Called when your app has been activated by the user selecting an action from a remote notification.118 // A nil action identifier indicates the default action.119 // You should call the completion handler as soon as you've finished handling the action.120 - (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler121 {122 123 NSLog(@"ios9新增 --- %@ -- %@ --- %@",identifier,userInfo,responseInfo);124 125 126 }//NS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED;127 128 129 // Called when your app has been activated by the user selecting an action from a remote notification.130 // A nil action identifier indicates the default action.131 // You should call the completion handler as soon as you've finished handling the action.132 - (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler133 {134 135 NSLog(@"ios8新增 --- %@ -- %@ --- %@",identifier,userInfo,application);136 137 138 }//NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;139 140 141 142 143 @end
5)都搞好了,我們得類比發推送。這裡我們用這個測試程式:https://github.com/shaojiankui/SmartPush 把我們剛剛產生的推送認證下載下來, 然互拖過來:
然後串連伺服器,再點擊推送就OK~~~~~~
可能出現的問題:
1)註冊失敗Error Domain=NSCocoaErrorDomain Code=3000 "未找到應用程式的“aps-environment”的授權字串"
這是因為推動認證設定錯誤了.而且會調用 遠程推送註冊失敗這個方法(- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error);首先檢查:三個認證的APPID是否是對的;三個認證是否都雙擊安裝了;APPID的ID是否為項目的Bundle Identifier; 基本也就這些情況吧, 有其他情況的可以評論補充.