IOS push (using Aurora push) and ios Aurora
This article is mainly based on the Aurora push SDK package a rapid integration Aurora push class package (not like not to spray)
(1) First, let's talk about the push principles:
Push principle:
The working mechanism of Push can be summarized
In the figure, Provider refers to the Push server of an iPhone software. In this article, I will use. net as the Provider.
APNS is the abbreviation of Apple Push Notification Service (Apple Push server). It is an Apple server.
It can be divided into three stages.
Phase 1: the. net application packs the message to be sent and the identifier of the target iPhone and sends it to APNS.
Stage 2: APNS searches for the iPhone with the corresponding identifier in its registered Push Service iPhone list and sends the message to the iPhone.
Stage 3: The iPhone transmits the sent message to the corresponding application, and a Push notification is displayed according to the settings.
We can see from.
1. First, the application registers for message push.
2. for IOS and APNS servers, deviceToken is required. The application accepts deviceToken.
3. The application sends the deviceToken to the PUSH server.
4. The server program sends a message to the APNS service.
5. The APNS Service sends messages to the iPhone application.
Both the iPhone client and APNS, Provider and APNS, must be connected through a certificate. Below I will introduce several certificates used.
(2) Encapsulation of Aurora push SDK classes
2.1 create a class that inherits from NSObject and temporarily name it MyJPush
In the. h file, add the following methods:
/** Register JPush */
+ (Void) registerJPush :( NSDictionary *) launchOptions;
/** Add listener */
+ (Void) addJPushListener :( id <CoreJPushProtocol>) listener;
/** Remove listener */
+ (Void) removeJPushListener :( id <CoreJPushProtocol>) listener;
/** Register alias and tags */
+ (Void) setTags :( NSSet *) tags alias :( NSString *) alias resBlock :( void (^) (BOOL res, NSSet * tags, NSString * alias) resBlock;
. M file implementation:
Macro variables are required:
# Define JPushAppKey @ "************" // APPKey pushed by Aurora
# Define JPushChannel @ "AppStore" // specifies the download channel of the application package
# Define JPushIsProduction NO // whether it is a production environment
/** Register JPush */
+ (Void) registerJPush :( NSDictionary *) launchOptions {
// Required
// You can add custom categories
[JPUSHService registerForRemoteNotificationTypes :( UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
Categories: nil];
// Required
// If You Need To Be compatible with earlier versions, use the [JPUSHService setupWithOption: launchOptions] method to initialize and use the pushConfig. plist file to declare appKey and other configuration content.
[JPUSHService setupWithOption: launchOptions appKey: JPushAppKey channel: JPushChannel apsForProduction: JPushIsProduction];
}
/** Add listener */
+ (Void) addJPushListener :( id <CoreJPushProtocol>) listener {
MyJPush * jpush = [MyJPush sharedCoreJPush];
If ([jpush. listenerM containsObject: listener]) return;
[Jpush. listenerM addObject: listener];
}
/** Remove listener */
+ (Void) removeJPushListener :( id <CoreJPushProtocol>) listener {
MyJPush * jpush = [MyJPush sharedCoreJPush];
If (! [Jpush. listenerM containsObject: listener]) return;
[Jpush. listenerM removeObject: listener];
}
-(NSMutableArray *) listenerM {
If (_ listenerM = nil ){
_ ListenerM = [NSMutableArray array];
}
Return _ listenerM;
}
-(Void) didReceiveRemoteNotification :( NSDictionary *) userInfo {
[Self handleBadge: [userInfo [@ "aps"] [@ "badge"] integerValue];
If (self. listenerM. count = 0) return;
[Self. listenerM enumerateObjectsUsingBlock: ^ (id <CoreJPushProtocol> listener, NSUInteger idx, BOOL * stop ){
If ([listener respondsToSelector: @ selector (didReceiveRemoteNotification :)]) [listener didreceiveremotenoication ication: userInfo];
}];
}
/** Process badge */
-(Void) handleBadge :( NSInteger) badge {
NSInteger now = badge-1;
[[UIApplication sharedApplication] cancelalllocalconfigurications];
[UIApplication sharedApplication]. applicationIconBadgeNumber = 0;
[UIApplication sharedApplication]. applicationIconBadgeNumber = now;
[JPUSHService setBadge: now];
}
+ (Void) setTags :( NSSet *) tags alias :( NSString *) alias resBlock :( void (^) (BOOL res, NSSet * tags, NSString * alias) resBlock {
MyJPush * jpush = [MyJPush sharedCoreJPush];
[JPUSHService setTags: tags alias: alias callbackSelector: @ selector (tagsAliasCallback: tags: alias :) object: jpush];
Jpush. ResBlock = resBlock;
}
-(Void) tagsAliasCallback :( int) iResCode tags :( NSSet *) tags alias :( NSString *) alias {
If (self. ResBlock! = Nil) self. ResBlock (iResCode = 0, tags, alias );
}
2.2 create a category file based on APPDelegate and temporarily name it AppDelegate + JPush
The following methods are implemented:
-(Void) application :( UIApplication *) application didRegisterForRemoteNotificationsWithDeviceToken :( NSData *) deviceToken {
// Required
[JPUSHService registerDeviceToken: deviceToken];
}
-(Void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo {
// Required, For systems with less than or equal to iOS6
[JPUSHService handleRemoteNotification: userInfo];
}
-(Void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo fetchCompletionHandler :( void (^) (UIBackgroundFetchResult) completionHandler {
// IOS 7 Support Required
[JPUSHService handleRemoteNotification: userInfo];
CompletionHandler (UIBackgroundFetchResultNewData );
CoreJPush * jpush = [CoreJPush sharedCoreJPush];
[Jpush didReceiveRemoteNotification: userInfo];
}
These two files can be directly used in your own project, and then add the following libraries to the project:
. CFNetwork. framework
. CoreFoundation. framework
. CoreTelephony. framework
. SystemConfiguration. framework
. Security. framework
. Libz. tbd
Step 2: configure the project:
. (1) The User Header Search Paths and Library Search Paths under Search Paths are '$ (PROJECT_DIR)/CoreJPush/Lib '.
. (2) select Project-Target-Capabilities-Background Modes and check Remote communications.
. (3) modify the Appkey of PushConfig. plist in the Common folder in CoreJPush framework to your Appkey.
. (4) if your project needs to support iOS systems smaller than 7.0, go to Build Settings to disable the bitCode option. Otherwise, compilation will fail.
. (5) Allow XCode7 to Support Http Transmission
If Xcode7 is used, manually add the following keys and values to the plist of the App Project to Support http transmission:
<Key> NSAppTransportSecurity </key>
<Dict>
<Key> NSAllowsArbitraryLoads </key>
<True/>
</Dict>
The most important step is:
1. Register JPush
Delete all push methods in your AppDelgate because CoreJPush is encapsulated internally.
# Import "MyJPush. h"
// Register JPush
[MyJPush registerJPush: launchOptions];
2. If you want to push data, you can use the following code:
// 1. Add a listener: This listener complies with the CoreJPushProtocol protocol.
[MyJPush addJPushListener: self];
// 2. You need to remove the listener in a proper place (such as dealloc)
[MyJPush removeJPushListener: self];
// 3. You have followed the MyJPushProtocol protocol and directly typed did in the. m file. Xcode will prompt you the following method:
-(Void) didReceiveRemoteNotification :( NSDictionary *) userInfo {
NSLog (@ "ViewController: % @", userInfo );
}
# Pragma mark-send a message on the Aurora push website, with parameters. After receiving the message, the mobile phone performs a series of operations.
// Obtain the push information (including the key and value added during the push, and the value obtained through the key)
-(Void) didReceiveRemoteNotification :( NSDictionary *) userInfo {
NSLog (@ "controller: % @", userInfo );
If ([userInfo. allKeys containsObject: @ "key"]) {// The key here is the key and value that you created when you sent a notification on the Aurora push platform.
NSLog (@ "notification sent successfully, start to jump ");
WebViewController * webVc = [[WebViewController alloc] init];
WebVc. UrlString = userInfo [@ "key"];
[Self. navigationController pushViewController: webVc animated: YES];
}
}
You can view the Aurora push documentation for other operations. encapsulate the two files to make it easier to configure and use later. There are also simple certificate configurations, online and Aurora push official documentation has a very clear description, here need to use Aurora push jpush-ios-2.1.5 later versions
If you have any need or correction, you can leave a message for me!
If you need source code, you can leave a message to give it away separately.