iOS:苹果推送(三)----一些示例代码和注意的问题

1. 推送必须用真机才能测试,并且要将设备的token传给服务器。appdelegate中的委托可以获取设备token。

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

{

    NSString *tokenStr = [NSString stringWithFormat:@"%@",deviceToken];

    tokenStr = [tokenStr stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];//将其中的<>去掉

    tokenStr = [tokenStr stringByReplacingOccurrencesOfString:@" " withString:@""];//将其中的空格去掉

//tokenStr 得到可用的token。

}


2. 程序启动时每次收到推送 或 程序未启动通过推送消息启动程序时,都将会触发appdelegate里面的方法(注意要跟服务器的证书一直,比如开发环境,服务器也要用开发证书的pem才行):

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{

//这里是收到推送后的逻辑代码


// 需要 #import <AudioToolbox/AudioToolbox.h>

        AudioServicesPlaySystemSound(1007); //系统的通知声音

        AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);//震动


//自定义声音

   NSString *path = [[NSBundle mainBundle] pathForResource:@"message" ofType:@"wav"];

    //组装并播放音效

    SystemSoundID soundID;

    NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];

    AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);

   AudioServicesPlaySystemSound(soundID);

   //声音停止

     AudioServicesDisposeSystemSoundID(soundID);

}

 

  • userInfo默认包含以下内容:

 aps =     {

       alert = "";//推送显示的问题信息在这里

       badge = 0;//app的icon右上角的推送数字 在这里设置

       sound = "";

   };

 

  • 如果需要添加自定义的字段,就让服务器的小伙伴们 跟aps同一层级添加一个数组(以Json为例):

{

  "aps" : { "alert" : "This is the alert text", "badge" : 1, "sound" : "default" },

  "server" : { "serverId" : 1, "name" : "Server name")

}
这样收到的
userInfo里面会多一个server的字段。


3. 如果需要代码控制BadgeNum(icon右上角的数字):

[UIApplication sharedApplication].applicationIconBadgeNumber 10;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值