- 什么是voip推送和普通的ios推送有什么区别呢?
普通推送分为:远程推送和本地推送,区别网上资料太多了,简单说一下,比如网易新闻,有什么大的新闻会在手机端接收到推送,这个就是远程推送,是把相关信息推送到苹果推送服务器-APNS,太简单了,不说了!
重点说下:voip的重点功能。
就是打视频或者语音电话的时候推送功能,一个典型的应用场景,A用户拨打B用户视频或者语音电话,A拨打,B铃声响起,此时A挂断,B铃声消失,就相当于控制对方手机一样。国外的语音视频电话都是采用voip push,比如whats app,line等语音视频通讯软件。
曾经有人说普通推送可以做到以上功能,我可以明确的给出回复:普通推送是无法做到的,必须用voip push才能做到。
注意事项:
1、证书的制作过程中也需要选中voip认证。
2、在xcode开发中也需要进行相关的设置,设置如下:
setting_xcode.png3、引入开发包
push_kit.png
xcode端代码如下:
1、首先注册为voip推送:
-(void)registerVoipNotifications{
PKPushRegistry * voipRegistry = [[PKPushRegistry alloc]initWithQueue:dispatch_get_main_queue()];
voipRegistry.delegate = self;
voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
UIUserNotificationType types = (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert);
UIUserNotificationSettings * notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication]registerUserNotificationSettings:notificationSettings];
}
在应用初始化方法中调用:
[self registerVoipNotifications];
2、获取token,用于发送到服务器端,服务器端发送推送消息时,会发送这个token到APNS服务器,APNS通过这个token定位到手机和对应的应用,进而推送消息。
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type{
NSString *str = [NSString stringWithFormat:@"%@",credentials.token];
NSString *_tokenStr = [[[str stringByReplacingOccurrencesOfString:@"<" withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"device_token is %@" , _tokenStr);
}
3、下面是最重要的一步:就是对推送过来的消息进行解析,可以在推送的时候设置标记,下面的代码是获取到标志以后,根据标志命令调用不同的方法进行处理,进而实现功能,注意这里接收到消息定义时,如果传过来的命令是call就创建一个本地推送,提示某人正在呼叫你,如果传过来的命令为cancel,就取消命令
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type{
UIUserNotificationType theType = [UIApplication sharedApplication].currentUserNotificationSettings.types;
if (theType == UIUserNotificationTypeNone)
{
UIUserNotificationSettings *userNotifySetting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:userNotifySetting];
}
NSDictionary * dic = payload.dictionaryPayload;
NSLog(@"dic %@",dic);
if ([dic[@"cmd"] isEqualToString:@"call"]) {
UILocalNotification *backgroudMsg = [[UILocalNotification alloc] init];
backgroudMsg.alertBody= @"You receive a new call";
backgroudMsg.soundName = @"ring.caf";
backgroudMsg.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] presentLocalNotificationNow:backgroudMsg];
}else if(([dic[@"cmd"] isEqualToString:@"cancel"]){
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification * wei = [[UILocalNotification alloc] init];
wei.alertBody= [NSString stringWithFormat:@"%ld 个未接来电",[[UIApplication sharedApplication]applicationIconBadgeNumber]];
wei.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber];
[[UIApplication sharedApplication] presentLocalNotificationNow:wei];
}
}
4、注意当进入应用时,角标清除:
- (void)applicationDidBecomeActive:(UIApplication *)application {
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
以上是IOS端代码
下面是服务器端发送:
服务器端代码如下:
1、获取推送服务这个是用的开源的java推送框架,github有源码:
/** * get push service
* @return
* @throws FileNotFoundException
*/
public ApnsService getPushService(Long accountId) throws FileNotFoundException {
String certPath = getCertPath(accountId);
logger.debug("cert path: "+certPath);
ApnsService service =
APNS.newService()
.withCert(certPath, "123456")
.withProductionDestination()
.build(); // service.testConnection();
return service;
}
2、推送命令包含在推送字段中
public void pushNotificationTest(Long accountId,String cloudpId,String content) throws Exception{
Relation relation = new Relation();
relation.setUserId(cloudpId);
relation.setAccountId(accountId);
// String deviceToken = queryDeviceToken(relation);
//logger.debug("deviceToken:"+deviceToken);
String deviceToken = handleDeviceToken("5e11eefc6f8b427b6646bcaf5320a5e714b121859f21b0ee78d88d3f411ac69b6");
ApnsService service = getPushServiceTest(accountId);
try{
String call = APNS.newPayload().customField("cmd","call").build(); String cancel = APNS.newPayload().customField("cmd","cancel").build();
service.push(deviceToken, call);
// service.push(deviceToken, cancel);
}catch (Exception e){
logger.error("push throw Exception: " +e.getMessage());
throw new Exception(e);
}finally {
if (service != null){
service.stop();
}
}
}
网友评论