实现推送功能APP端需要完成的工作
推送功能简介
实现推送的流程如下:

从APP注册推送功能,到APNS服务器发送推送消息给设备,有五个步骤。
一旦推送注册完成,应用自身的服务器以provider的身份提供推送。

APP端实现
在代码方面,推送的注册、监听和处理都集中在AppDelegate类里:
1.(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
在该方法体里主要实现两个功能:
一是完成推送功能的注册请求,即在程序启动时弹出是否使用推送功能;
二是实现的程序启动是通过推送消息窗口触发的,在这里可以处理推送内容;
1.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] init] autorelease];
self.window.rootViewController = self.viewController;
[self.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
[self.window makeKeyAndVisible]; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
//判断程序是不是由推送服务完成的,亦即通过用户点击推送通知完成的
if (launchOptions) {
NSDictionary* pushNotificationKey = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (pushNotificationKey) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@”推送通知”
message:@”这是通过推送窗口启动的程序,你可以在这里处理推送内容”
delegate:nil
cancelButtonTitle:@”知道了”
otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
return YES;
}
2. 接收从苹果服务器返回的唯一的设备token,该token是推送服务器发送推送消息的依据,所以需要发送回推送服务器保存
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString* token = [NSString stringWithFormat:@"%@",deviceToken];
NSLog(@”apns -> 生成的devToken:%@”, token);
//把deviceToken发送到我们的推送服务器
DeviceSender* sender = [[[DeviceSender alloc]initWithDelegate:self ]autorelease];
[sender sendDeviceToPushServer:token ];
}
3.接收注册推送通知功能时出现的错误,并做相关处理:
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(@”apns -> 注册推送功能时发生错误, 错误信息:\n %@”, err);
}
4. 接收到推送消息,解析处理
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@”\napns -> didReceiveRemoteNotification,Receive Data:\n%@”, userInfo);
//把icon上的标记数字设置为0,
application.applicationIconBadgeNumber = ;
if ([[userInfo objectForKey:@"aps"] objectForKey:@”alert”]!=NULL) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@”**推送消息**”
message:[[userInfo objectForKey:@"aps"] objectForKey:@”alert”]
delegate:self
cancelButtonTitle:@”关闭”
otherButtonTitles:@”处理推送内容”,nil];
alert.tag = alert_tag_push;
[alert show];
}
}
通过上面的代码,基本推送功能的开发已经完成了,需要推送服务器支持,请自行解决。
实现推送功能APP端需要完成的工作的更多相关文章
- mqtt协议实现 java服务端推送功能(三)项目中给多个用户推送功能
接着上一篇说,上一篇的TOPIC是写死的,然而在实际项目中要给不同用户 也就是不同的topic进行推送 所以要写活 package com.fh.controller.information.push ...
- erlang-百度云推送Android服务端功能实现-erlang
百度云推送官方地址http://developer.baidu.com/wiki/index.php?title=docs/cplat/push 简单的介绍下原理: 百度云推送支持IOS和Androi ...
- APP如何实现推送功能
一.推送功能的集成 (1)在Umeng开发者中心,申请新应用,开通推送功能.此时需要上传开发推送证书和生产推送证书的p12文件. 申请证书的流程如下: >>1 创建开发推送证书 >& ...
- 关于ios 推送功能的终极解决
刚刚做了一个使用推送功能的应用 遇到了一些问题整的很郁闷 搞了两天总算是弄明白了 特此分享给大家 本帖 主要是针对产品发布版本的一些问题 综合了网上一些资料根据自己实践写的 不过测试也可以看看 首先要 ...
- 如何用好消息推送为app拉新、留存、促活
作为移动端APP产品运营最重要的运营手段,JPush消息推送被越来越多的APP厂商所重视,在信息泛滥的移动互联网时代,手机APP应用安装得越来越多,小小的手机屏幕每天收到的消息推送也越来越多,站在用户 ...
- [转]关于ios 推送功能的终极解决
刚刚做了一个使用推送功能的应用 遇到了一些问题整的很郁闷 搞了两天总算是弄明白了 特此分享给大家 本帖 主要是针对产品发布版本的一些问题 综合了网上一些资料根据自己实践写的 不过测试也可以看看 首先要 ...
- 转:关于ios 推送功能的终极解决
刚刚做了一个使用推送功能的应用 遇到了一些问题整的很郁闷 搞了两天总算是弄明白了 特此分享给大家 本帖 主要是针对产品发布版本的一些问题 综合了网上一些资料根据自己实践写的 不过测试也可以看看 首先要 ...
- 史上最全面的SignalR系列教程-3、SignalR 实现推送功能-集线器类实现方式
1.概述 通过前两篇 史上最全面的SignalR系列教程-1.认识SignalR 史上最全面的SignalR系列教程-2.SignalR 实现推送功能-永久连接类实现方式 文章对SignalR的介绍, ...
- Android之使用个推实现三方应用的推送功能
PS:用了一下个推.感觉实现第三方应用的推送功能还是比较简单的.官方文档写的也非常的明确. 学习内容: 1.使用个推实现第三方应用的推送. 所有的配置我最后会给一个源代码,内部有相关的配置和 ...
随机推荐
- struct2访问或添加request/session/application
访问或添加request/session/application 1 通过ActionContext //这样放置 public String execute() { ActionConte ...
- linux源代码阅读笔记 free_page_tables()分析
/* 77 * This function frees a continuos block of page tables, as needed 78 * by 'exit()'. As does co ...
- rabbitMQ安装及部署
1.安装 rpm -ivh erlang-18.3-1.el6.x86_64.rpm, 下载地址:http://www.rabbitmq.com/releases/erlang/ rpm --impo ...
- 1> Strut2 Mapping to MVC
CONTROLLER—FILTERDISPATCHER We’ll start with the controller. It seems to make more sense to start th ...
- POJ 2029 Get Many Persimmon Trees(DP||二维树状数组)
题目链接 题意 : 给你每个柿子树的位置,给你已知长宽的矩形,让这个矩形包含最多的柿子树.输出数目 思路 :数据不是很大,暴力一下就行,也可以用二维树状数组来做. #include <stdio ...
- REST_FRAMEWORK加深记忆-加了用户登陆认证,自定义权限的API接口
哈哈,终于快结束了.. urls.py from django.conf.urls import include, url from django.contrib import admin urlpa ...
- FileOutputStream和FileInputStream
package one.string; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFound ...
- lintcode:Add Binary 二进制求和
题目: 二进制求和 给定两个二进制字符串,返回他们的和(用二进制表示). 样例 a = 11 b = 1 返回 100 解题: 和求两个链表的和很类似 考虑进位,考虑最后一项的进位 0+0 = 0 不 ...
- [topcoder]PackingBallsDiv2
http://community.topcoder.com/stat?c=problem_statement&pm=12995 简单题 class PackingBallsDiv2 { pub ...
- redhat 7.2 配置yum源
http://blog.csdn.net/wylfengyujiancheng/article/details/50418930