Apple native push, Apple

Source: Internet
Author: User

Apple native push, Apple

I wrote a demo a long time ago ~~~ It may be a bit wrong

GIthub: https://github.com/BigShow1949/YFPushTest

Steps:

1) Add the app id.

Apple Developer Center: Identifiers ---> App IDs add an ID, such as my Name: YFPushTest. The ID here must be the Bundle Identifier of the project, such as com. YFPushTest. BigShow. YFPushTest;

2) generate an apple push certificate (Production & Testing)

For example: com. YFPushTest. BigShow. YFPushTest. When selecting the app id, select the one we just created and do not select another one (many AppID are easy to forget)

3) generate the description file

Name: YFPushTestProfile. Similarly, do not select an incorrect AppID.

4) code on:

1 # import "AppDelegate. h "2 3 @ interface AppDelegate () 4 5 @ end 6 7 @ implementation AppDelegate 8 9 10-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions 11 {12 // application. applicationIconBadgeNumber = 0; 13 14 if ([UIDevice currentDevice]. systemVersion. doubleValue >=8.0) 15 {16 // 1. request Authorization can send a notification to the user 17 UIUserNotificationSettings * settin Gs = [UIUserNotificationSettings Syntax: uiusernotiftypetypesound | uiusernotiftypetypebadge | UIUserNotificationTypeAlert categories: nil]; 18 19 [application registerUserNotificationSettings: settings]; 20} 21 else 22 {23 [application Syntax: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound]; 24} 25 26 if (la UnchOptions [UIApplicationLaunchOptionsRemoteNotificationKey]) 27 {28 // jump 29} 30 31 return YES; 32} 33 34 35/** 36 * remote push registration successful 37*38 * @ param deviceToken 39 */40-(void) application :( UIApplication *) application metadata :( NSData *) deviceToken 41 {42 // 25bb75ac 3ffcebd7 90d9f517 1ebca904 154a367a 87781e5d b9ea288e 37fdf487 43 NSLog (@"----- DeviceToken ---- % @ devDesc --- % @ ", deviceToken, deviceToken. description); 44 45 // tokenStr to obtain available tokens. 46 NSString * tokenStr = [NSString stringWithFormat: @ "% @", deviceToken]; 47 tokenStr = [tokenStr failed: [NSCharacterSet characterSetWithCharactersInString: @ "<>"]; // remove <> 48 tokenStr = [tokenStr stringByReplacingOccurrencesOfString: @ "" withString: @ ""]; // remove the space from the Space: 49 50 NSLog (@ "token --- % @", tokenStr); 51 // registration successful, return token 52 // UIAlertView * alert = [[UIAlertView alloc] ini TWithTitle: @ "Token returned by APNS:" message: tokenStr delegate: self cancelButtonTitle: @ "cancel" otherButtonTitles: @ "OK", nil]; 53 // 54 // [alert show]; 55 56 // applicationIconBadgeNumber 57} 58 59 60/** 61 * Registration failed 62*63 */64-(void) application :( UIApplication *) application didFailToRegisterForRemoteNotificationsWithError :( NSError *) error 65 {66 67 NSLog (@ "registration failed ---- % @", error); 68 NSString * tokenStr = [N SString stringWithFormat: @ "% @", error]; 69 UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "registration failed! "Message: tokenStr delegate: self cancelButtonTitle: @" cancel "otherButtonTitles: @" OK ", nil]; 70 [alert show]; 71 NSLog (@" registration failed % @", error); 72 73 74} // NS_AVAILABLE_IOS (3_0); 75 76 77 78-(void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo 79 {80 NSLog (@ "push notification received ---- % @", userInfo); 81 82/* 83 84 "name": "YFPushTest", 85 "action_type ": "1", 86 "message": "Your vehicle jingkkkkkk drove into Handan on", 87 "aps": {88 "alert": "This is some fancy message. ", 89" badge ": 1, 90" sound ":" Your vehicle jingkkkkk drove into Handan on "91}; 92 93 */94 95 // application. applicationIconBadgeNumber-= 1; 96 // display the PUSH message in the form of alert 97 NSString * message = [[userInfo objectForKey: @ "aps"] objectForKey: @ "alert"]; 98 99 UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "prompt" message: message delegate: self cancelButtonTitle: @ "cancel" otherButtonTitles: @ "OK", 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 *) icationicationsettings107 {108 109 NSLog (@ "ls ---- % @", icationicationsettings); 110 111 // 2. register remote notification 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 shocould 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 added --- % @ -- % @ --- % @", 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 shocould 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 (^ )()) completionhandlerance {134 135 NSLog (@ "added ios8 --- % @ -- % @ --- % @", identifier, userInfo, application); 136 137 138} // NS_AVAILABLE_IOS (8_0) _ TVOS_PROHIBITED; 139 140 141 142 @ end

5) We have to simulate push. Here we use this test program: https://github.com/shaojiankui/SmartPush to download the push certificate we just generated, but drag each other over:

Connect to the server and click push ~~~~~~

 

Possible problems:

1) Registration failed Error Domain = NSCocoaErrorDomain Code = 3000 "the application's" aps-environment "authorization string is not found"

This is because the pushing certificate settings are incorrect. in addition, the method (-(void) application :( UIApplication *) application didFailToRegisterForRemoteNotificationsWithError :( NSError *) error will be called; first, check whether the APPID of the three certificates is correct; check whether the three certificates have been installed by double-click; whether the ID of APPID is the Bundle Identifier of the project; this is basically the case. If there are other situations, you can comment and add them.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.