先要配置好相应的证书,之后
在APPDelegate文件中设置
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
......
//消息推送注册
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge];
//判断程序是不是由推送服务完成的
if (launchOptions) {
NSDictionary* pushNotificationKey = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (pushNotificationKey) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"推送通知"
message:@"这是通过推送窗口启动的程序,你可以在这里处理推送内容"
delegate:nil
cancelButtonTitle:@"知道了"
otherButtonTitles:nil, nil];
[alert show];
}
}
......
}
向苹果注册推送通知
实现推送通知代理
#pragma mark ------------- 消息推送代理 -----------------
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// NSString *token = [NSString stringWithFormat:@"%@", deviceToken];
NSString *token=[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
token=[[token description] stringByReplacingOccurrencesOfString:@" " withString:@""];
// user_defaults_set_string(@"deviceToken",STRING_FORMAT(@"%@",token));
//获取终端设备标识,这个标识需要通过接口发送到服务器端,服务器端推送消息到APNS时需要知道终端的标识,APNS通过注册的终端标识找到终端设备。
NSLog(@"My token is---------:%@", token);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSString *error_str = [NSString stringWithFormat: @"%@", error];
NSLog(@"Failed to get token, error:%@", error_str);
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
/**********震动****************/
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
/**********提示音****************/
AudioServicesPlaySystemSound(107);
//在此处理接收到的消息。
NSLog(@"Receive remote notification ------------------ : %@",userInfo);
/************处理消息************/
}