Apns is a message push mechanism for iOS. For more information, see the final reference list. Here we emphasize some key steps.
1. Generate a certificate.
Interacting with Apple's apns server involves iOS devices and providers that provide push services. Both require certificate verification. IOS devices interact with apns through the underlying layer, and applicationsProgramYou do not need to reference any certificate, but whether your application can successfully register a Message notification is related to the bundle identifier in your project, the appid under provisioning profile of the bundle identifier you specified must activate "enable for Apple push notification service", for example:
As shown in the figure, development and production are separated. When the provider certificate is generated, if the development environment generates the development certificate, if the production environment generates the production certificate.
Bundle identifier --> provision profile --> appid --> enable for Apple push notification service --> push SSL Certificate
It can be seen that although the iOS app does not need to load the downloaded certificate, it actually exists with the corresponding certificate, only identified by the bundle identifier.
Note: If you have not activated apns for the corresponding appid, You need to download the new provision profile and reinstall it. The method to reinstall the profile is very simple. Open the organizer window, delete the corresponding provision profile file and drag the newly downloaded file to the window.
If you want to pass the debugging in the application, you need to make some configuration; otherwise, the following error occurs:
"No valid APS-Environment entitlement found for application"
Open the new provision profile file just downloaded, which contains a configuration section:
< Key > Entitlements </ Key >
< Dict >
< Key > Application-identifier </ Key >
< String > 72.16xdwf8l.com. XXX. appname </ String >
< Key > APS-Environment </ Key >
< String > Development </ String >
< Key > Get-task-allow </ Key >
< True />
< Key > Keychain-access-groups </ Key >
< Array >
< String > 7c57xdwf8l .* </ String >
</ Array >
</ Dict >
We need to addTo configure entitlements, follow these steps:
1. Enable and add configuration.
. Xcodeproj --> summary -->Entitlements Section --> select "enableEntitlements"
$ {Product_name} is automatically generated }.Entitlements file, add key-value pairs to the file:
key |
type |
value |
|
Get-task-allow |
Bollean |
Yes |
|
APS-Environment |
String |
Development |
This line is required |
Application-identifier |
String |
7c57xdwxxx.com. XXX. appname |
|
Keychain-access-groups |
array |
Item0String7c57xdwxxx .* |
|
2. You can also specifyEntitlements. Entitlements FileTo add a file:
New files --> code siging -->Entitlements --> next...
File Content is the same as above. Select this file in configuration.
3. subscribe and unsubscribe.
If an iOS device subscribes to or unsubscribes to a Message notification from apns, you can click * appdelegate. in M, the application: didfinishlaunchingwitexceptions: method is used. You can also use the * viewcontroller. m in a trigger event.
Subscription:
[[Uiapplication sharedapplication] registerforremotenotificationtypes :( uiremotenotificationtypebadge|
Uiremotenotificationtypesound|Uiremotenotificationtypealert)];
Unsubscribe:
[[Uiapplication sharedapplication] unregisterforremotenotifications];
If you want to avoid subscribing to and receiving devicetoken at each startup, you can make the following judgment:
If([[Uiapplication sharedapplication] enabledremotenotificationtypes]
{
[[Uiapplication sharedapplication] registerforremotenotificationtypes :( uiremotenotificationtypebadge|
Uiremotenotificationtypesound|Uiremotenotificationtypealert)];
}
This may only subscribe to and receive the devicetoken when the program is installed for the first time.Unregisterforremotenotifications will not be executed again.
4. Delegate method.
The delegate method is triggered when the registerforremotenotifications method is called or when the application receives a notification from apns. Because these delegate methods are defined in uiapplicationdelegate, the implementation must be implemented in * appdelegate. M class. For example:
// Register PUSH Service
- ( Void ) Application :( uiapplication * ) Application didregisterforremotenotificationswithdevicetoken :( nsdata * ) Devicetoken {
Nsstring * Token = [[Devicetoken description] stringbytrimmingcharactersinset:
[Nscharacterset charactersetwithcharactersinstring: @" <> " ]; // Remove "<>"
Token = [[Token description] stringbyreplacingoccurrencesofstring: @" " Withstring: @"" ]; // Remove Spaces
Nslog ( @" Devicetoken: % @ " , Token );
}
// An error occurred while registering the PUSH Service.
- ( Void ) Application :( uiapplication * ) Application didfailtoregisterforremotenotificationswitherror :( nserror * ) Error {
Nslog ( @" Error in registration for apns. Error: % @ " , Error );
}
// Receive push messages
- ( Void ) Application :( uiapplication * ) Application didreceiveremotenotification :( nsdictionary * ) Userinfo {
Nslog ( @" Received push message: % @ " , [[Userinfo objectforkey: @" APS " ] Objectforkey: @" Alert " ]);
Uialertview * Alert = [[Uialertview alloc] initwithtitle: @" Push notification "
Message: [[userinfo objectforkey: @" APS " ] Objectforkey: @" Alert " ]
Delegate : Self
Cancelbuttontitle: @" Close "
Otherbuttontitles: @" Update Status " , Nil];
[Alert show];
[Alert release];
}
Note: Application: didregisterforremotenotificationswithdevicetoken: after obtaining the devicetoken, you must push it to the provider that provides the PUSH Service. The provider saves the token corresponding to the uuid of the Application for use when sending messages.
To generate the certificate required by. net, follow these steps.
2. Provider Development.
You can useApns-Sharp is an open-source library for development. There are also many relatedArticleDescribe its development principles in detail, such as the following articles:
Local and push notification programming guide: about local communications and push communications
Local and push notification programming guide: provider communication with Apple push notification service
Programming Apple push Notification Services
Apple push notification service
In addition, pushmebaby is provided for development and testing under xcode.