ios 推送 问题

首先注册通知:

=.=在iOS8中注册通知的方式略有不同如下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //注册通知
    if ([UIDevice currentDevice].systemVersion.doubleValue<8.0) {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
    }else
    {
        [[UIApplication sharedApplication] registerForRemoteNotifications];
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]];
    }
    
    //判断是否由远程消息通知触发应用程序启动
    if (launchOptions) {
        //获取应用程序消息通知标记数(即小红圈中的数字)
        NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
        if (badge>0) {
            //如果应用程序消息通知标记数(即小红圈中的数字)大于0,清除标记。
            badge--;
            //清除标记。清除小红圈中数字,小红圈中数字为0,小红圈才会消除。
            [UIApplication sharedApplication].applicationIconBadgeNumber = badge;
            NSDictionary *pushInfo = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
            
            //获取推送详情
            NSString *pushString = [NSString stringWithFormat:@"%@",[pushInfo  objectForKey:@"aps"]];
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"finish Loaunch" message:pushString delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil];
            [alert show];
        }
    }
    return YES;
}

获得deviceToken,并将其保存在自己的服务器上。

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    //注册成功,将deviceToken保存到应用服务器数据库中
    NSString *token = [NSString stringWithFormat:@"%@", deviceToken];
 NSString *tempdeviceToken = [[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""];
NSLog(@"My token is:%@", token);
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{ 
//注册token失败 
NSString *error_str = [NSString stringWithFormat: @"%@", error]; NSLog(@"Failed to get token, error:%@", error_str);
}
处理消息

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{ 
// 处理推送消息 
NSLog(@"userinfo:%@",userInfo); 
NSLog(@"收到推送消息:%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
}



第二步:
在pushmebaby里:

每次推送时访问服务器,将服务器上保存下来的devicetoken读取出来,这样就可以一个一个的将通知推送到每个设备了。


应该注意,尽量将多个消息放到一个连接里发送,与服务器保持长连接,不能发一个消息连接一次,连接过于频繁,服务器可能会把你的IP暂时禁掉。

在设备上获得Token,开发环境下和从App Store下载正式的应用,获得的Token是一样的。


推送内容的设置:

所能推送内容的长度是256个字节,
只能推送256个数字或者英文字母,或者是128个汉字.

{
\"aps\":
{
\"alert\":\"You!\",------------要推送的文字信息
\"newsurl\":\"http://www.baidu.com\",------------推送内容链接
\"badge\":5,------------下图标右上角的提示数字
\"sound\":\"beep.wav\"------------提示的声音
}
}

{
\"aps\":
{
\"alert\":\"You!\",
\"newsurl\":\"http://www.baidu.com\",
\"badge\":5,
\"sound\":\"beep.wav\"
}

}


参考博客:

http://blog.youkuaiyun.com/think12/article/details/8863411#comments

http://www.cnblogs.com/E7868A/archive/2012/12/14/2791493.html

这个博客更详细

http://blog.youkuaiyun.com/jiajiayouba/article/details/39926017

在获得推送消息后跳转到指定位置,参考博客 http://segmentfault.com/q/1010000002447015


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值