推送通知

来源:互联网 发布:微信淘宝优惠群怎么做 编辑:程序博客网 时间:2024/05/16 10:45

一 本地通知:

1.计划通知开始:

- (IBAction)scheduleStart:(id)sender

{

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

localNotification.fireDate = [[NSDate alloc] initWithTimeIntervalSinceNow:10];//设置通知10秒后触发

localNotification.alertBody = @"计划通知,新年好!";//设置通知消息

localNotification.applicationIconBadgeNumber = 1;//设置通知标记数

localNotification.soundName = UILocalNotificationDefaultSoundName;//设置通知出现时的声音

localNotification.alertAction = @"View Details";//设置动作按钮的标题

[[UIApplication sharedApplication] scheduleLocalNotificationNotification:localNotification];//计划通知

/**[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];//立即发送通知*/

}

2.停止所有计划通知

- (IBAction)scheduleEnd:(id)sender

{

[[UIApplication sharedApplication] cancelAllLocalNotifications];//结束所有通知

}

二 推送通知(发送者-APNS(Apple Push Notification Service)-用户)

1.通过SSL证书导出p12文件。

2.客户端实现:

application:didFinishLanchingWithOptions:应用启动方法,在这个方法注册接收通知的类型和图标上的标记

application:didRegisterForRemoteNotificationsWithDeviceToken:注册成功回调,要把deviceToken发送给服务器端

application:didFailToRegisterForRemoteNotificationsWithError:注册失败回调

application:didReceiveRemoteNotification:接收推送通知

3.服务器端实现:

如果要编写内容提供者的推送服务程序,需要进行SSL认证编程,以及构建APNS数据包,数据包分为3个主要部分:Command(命令)、deviceToken(令牌)和Payload(载荷),载荷不能超过256字节,是JSON格式,例如:

{“apns”:{

"alert":"You got your emails.",

"badge":9,

"sound":"bingeing.aiff"

}

}

推送程序可以用很多语言实现,比如php,java等.需要提供给服务器pem文件。

0 0
原创粉丝点击