如何写通告完成通告 ?

43579人阅读
推送通知的步骤:1、询问是否允许推送通知。2、如果用户允许在APPDELEGATE 中实现
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
3、将token发送到服务器上
4、服务器收到toke后 发送推送通知,客户端相应该推送同通知
代码如下://每次唤醒- (void)applicationDidBecomeActive:(UIApplication *)application
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
//每次醒来都需要去判断是否得到device token
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(registerForRemoteNotificationToGetToken) userInfo:nil repeats:NO];
//hide the badge
application.applicationIconBadgeNumber = 0;
[[CheckVersion sharedCVInstance] checkVersionOfServer];
[[AnalyticsUtil sharedAnalyticsUtil] appLaunch];
AnalyticsJSONElement *viewElement = [[AnalyticsJSONElement alloc] init];
viewElement.jsonType = AnalyticsJSONTypeV
viewElement.typeID = @&0&;
[[AnalyticsUtil sharedAnalyticsUtil] postAnalyticsMsgToServerWithElement:viewElement];
[viewElement release];
- (void)applicationWillTerminate:(UIApplication *)application
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
#pragma mark -
#pragma mark - Getting Device token for Notification support
//向服务器申请发送token 判断事前有没有发送过
- (void)registerForRemoteNotificationToGetToken
NSLog(@&Registering for push notifications...&);
//注册Device Token, 需要注册remote notification
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if (![userDefaults boolForKey:DeviceTokenRegisteredKEY])
//如果没有注册到令牌 则重新发送注册请求
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeNewsstandContentAvailability |
UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound)];
//将远程通知的数量置零
dispatch_async(dispatch_get_global_queue(0,0), ^{
//1 hide the local badge
if ([[UIApplication sharedApplication] applicationIconBadgeNumber] == 0) {
// [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
//2 ask the provider to set the BadgeNumber to zero
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *deviceTokenStr = [userDefaults objectForKey:DeviceTokenStringKEY];
[self resetBadgeNumberOnProviderWithDeviceToken:deviceTokenStr];
//允许的话 自动回调的函数
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
//将device token转换为字符串
NSString *deviceTokenStr = [NSString stringWithFormat:@&%@&,deviceToken];
//modify the token, remove the
deviceTokenStr
-&%@&, [deviceTokenStr length], [[deviceTokenStr substringWithRange:NSMakeRange(0, 72)] substringWithRange:NSMakeRange(1, 71)]);
deviceTokenStr = [[deviceTokenStr substringWithRange:NSMakeRange(0, 72)] substringWithRange:NSMakeRange(1, 71)];
NSLog(@&deviceTokenStr = %@&,deviceTokenStr);
//将deviceToken保存在NSUserDefaults
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//保存 device token 令牌,并且去掉空格
[userDefaults setObject:[deviceTokenStr stringByReplacingOccurrencesOfString:@& & withString:@&&] forKey:DeviceTokenStringKEY];
//send deviceToken to the service provider
dispatch_async(dispatch_get_global_queue(0,0), ^{
//没有在service provider注册Device Token, 需要发送令牌到服务器
if ( ![userDefaults boolForKey:DeviceTokenRegisteredKEY] )
NSLog(@& 没有 注册Device Token&);
[self sendProviderDeviceToken:deviceTokenStr];
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSString *str = [NSString stringWithFormat: @&Error: %@&, err];
NSLog(@&获取令牌失败:
//如果device token获取失败则需要重新获取一次
//[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(registerForRemoteNotificationToGetToken) userInfo:nil repeats:NO];
//获取远程通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@&received badge number ---%@ ----&,[[userInfo objectForKey:@&aps&] objectForKey:@&badge&]);
for (id key in userInfo) {
NSLog(@&key: %@, value: %@&, key, [userInfo objectForKey:key]);
NSLog(@&the badge number is
[[UIApplication sharedApplication] applicationIconBadgeNumber]);
NSLog(@&the application
badge number is
application.applicationIconBadgeNumber);
application.applicationIconBadgeNumber += 1;
// We can determine whether an application is launched as a result of the user tapping the action
// button or whether the notification was delivered to the already-running application by examining
// the application state.
//当用户打开程序时候收到远程通知后执行
if (application.applicationState == UIApplicationStateActive) {
// Nothing to do if applicationState is Inactive, the iOS already displayed an alert view.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@&温馨提示&
message:[NSString stringWithFormat:@&\n%@&,
[[userInfo objectForKey:@&aps&] objectForKey:@&alert&]]
delegate:self
cancelButtonTitle:@&OK&
otherButtonTitles:nil];
dispatch_async(dispatch_get_global_queue(0,0), ^{
//hide the badge
application.applicationIconBadgeNumber = 0;
//ask the provider to set the BadgeNumber to zero
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *deviceTokenStr = [userDefaults objectForKey:DeviceTokenStringKEY];
[self resetBadgeNumberOnProviderWithDeviceToken:deviceTokenStr];
[alertView show];
[alertView release];
// http://192.168.11.24/ClientInterface.ashx?action= savetoken&clientid=&token=
#pragma mark -
#pragma mark - Getting Device token for Notification support
//发送token
- (void)sendProviderDeviceToken: (NSString *)deviceTokenString
// Establish the request
NSLog(@&sendProviderDeviceToken = %@&, deviceTokenString);
NSString *UDIDString = [[UIDevice currentDevice] uniqueIdentifier];
NSString *body = [NSString stringWithFormat:@&action=savetoken&clientid=%@&token=%@&, UDIDString, deviceTokenString];
NSString *baseurl = [NSString stringWithFormat:@&%@?&,URL_OF_PUSH_NOTIFICATION_SERVER];
//服务器地址
NSLog(@&send provider device token = %@&, baseurl);
NSURL *url = [NSURL URLWithString:baseurl];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod: @&POST&];
[urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setValue:@&application/x-www-form-urlencoded& forHTTPHeaderField:@&Content-Type&];
NSURLConnection *tConnection = [[NSURLConnection alloc] initWithRequest: urlRequest delegate: self];
self.deviceTokenConnetion = [tConnection retain];
[tConnection release];
#pragma mark -
#pragma mark - reset Badge Number
- (void)resetBadgeNumberOnProviderWithDeviceToken: (NSString *)deviceTokenString
reset Provider DeviceToken %@&, deviceTokenString);
isNotificationSetBadge = YES;
// Establish the request
NSString *body = [NSString stringWithFormat:@&action=setbadge&token=%@&, [deviceTokenString stringByReplacingOccurrencesOfString:@& & withString:@&&]];
NSString *baseurl = [NSString stringWithFormat:@&%@?&, URL_OF_PUSH_NOTIFICATION_SERVER];
//服务器地址
NSURL *url = [NSURL URLWithString:baseurl];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod: @&POST&];
[urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setValue:@&application/x-www-form-urlencoded& forHTTPHeaderField:@&Content-Type&];
NSURLConnection *tConnection = [[NSURLConnection alloc] initWithRequest: urlRequest delegate: self];
self.deviceTokenConnetion = [tConnection retain];
[tConnection release];
#pragma mark -
#pragma mark - NSURLConnection delegate function
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
NSHTTPURLResponse *resp = (NSHTTPURLResponse *)
NSLog(@&Response statusCode:
%d&, resp.statusCode);
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
NSString *rsp = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@&connection
Received data = %@
//if the string from provider is &true&, means the devicetoken is stored in the provider server
//so the app won't send the devicetoken next time.
if (isNotificationSetBadge == NO) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if([rsp isEqualToString:@&true&])
NSLog(@&connection
Received data = %@
[userDefaults setBool:YES forKey:DeviceTokenRegisteredKEY];
}else{//isNotificationSetBadge == YES;
NSLog(@&connection
isNotificationSetBadge = NO;
[rsp release];
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
NSLog(@&connection
Did Finish Loading &);
[self.deviceTokenConnetion cancel];
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:368634次
积分:2607
积分:2607
排名:第10624名
原创:11篇
转载:52篇
评论:21条
(2)(3)(1)(2)(2)(1)(4)(1)(2)(2)(8)(1)(1)(1)(3)(4)(3)(4)(6)(12)本类热门推荐
本站所有内容均来源于互联网,本站不负任何法律责任,如果侵犯您的版权,请及时告知我们 ,我们将在收到通知后72小时内删除
牛游戏网提供游戏(包括单机游戏,电视游戏,手机游戏,网页游戏,网络游戏等)资讯,下载,攻略,补丁,始终站在 的最前沿
CopyRight(C) WWW.NEWYX.NET All Right Reserved 湘ICP备号
牛游戏网温馨提示:适度游戏娱乐 沉迷游戏伤身 合理安排时间 享受单机游戏以上由提供
当前位置:
> 详细页面
Windows7系统IE下载完成后没有发出通知怎么解决?
时间: 12:00来源:作者:咕噜噜
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
系统教程栏目
热门系统教程
在笔记本电脑早已普及到会议室的这个年代,商务人士拿笔记...
热门系统下载
最新系统教程
热门软件下载
Copyright&2011 系统之家(www.xitongzhijia.net) 版权所有 闽ICP备号-1
本站发布的系统与软件仅为个人学习测试使用,请在下载后24小时内删除,不得用于任何商业用途,否则后果自负,请支持购买微软正版软件!
如侵犯到您的权益,请及时通知我们,我们会及时处理。

参考资料

 

随机推荐