iOS: Some issues about push notification

zz/2024/5/21 21:01:16

custom data in push notification

http://no001.blog.51cto.com/1142339/642817


当运行带apns feature的app到真机上后,会弹出错误:

"Error Domain=NSCocoaErrorDomain Code=3000 "no valid 'aps-environment' entitlement string found for application" UserInfo=0x15b200 {NSLocalizedDescription=no valid 'aps-environment' entitlement string found for application}" error.

是什么原因?

检查过下面几点:

1. App ID 已经enable了apns service

2. Provisioning profile的创建是在app id enable apns service之后!

3. 进行调试的真机已经包含在provisioning profile里

4. 没办法,死马当活马医,删除了旧的provisioning profile,再创建一个新的,添加新的到xcode organizer window


依然没解决上面的问题。后来仔细看,原来是"building setting" tab里的"code signing identity"我设置错了


上图中2个可选择的item的name都一样,但仔细看它们所属的provisioning profile不同(见灰化部分),我错误的选择了iOS team provisioning profile: * (for application identifiers *),应该选择的是Mobile CAP Provisioning profile那个


在iphone锁机的状态下分3种情况:

1) 你的app没有运行。这时如果来了push message,

解锁后,就会自动运行该app。执行到application:didFinishLaunchingWithOptions:方法时notification info包含在参数launchOptions里

2) 你的app运行在background。这时如果来了push message,

解锁后,自动切换该app,并以notification info作为参数调用application:didReceiveRemoteNotification:方法


3) 你的app运行在foreground。这时如果来了push message,

解锁后,会以notification info作为参数调用application:didReceiveRemoteNotification:方法



在iphone不是锁机的状态下分3种情况

1) 你的app没有运行。这时如果来了push message,

> 如果你click "Launch" in alert notification window,或是在notification area里click 与你的app相关的notification,那么就会运行该app,执行到application:didFinishLaunchingWithOptions:方法时notification info包含在参数launchOptions里

> 如果你click "Close" in alert notification window,或是没有在notification area里click 与你的app相关的notification,而是直接click app icon来打开app,执行到application:didFinishLaunchingWithOptions:方法时notification info并不会包含在参数launchOptions里

2) 你的app运行在background。这时如果来了push message,

> 如果你click "Launch" in alert notification window,或是在notification area里click 与你的app相关的notification,那么就会切换到app,并以notification info作为参数调用application:didReceiveRemoteNotification:方法

> 如果你click "Close" in alert notification window,或是没有在notification area里click 与你的app相关的notification,而是直接click app icon来打开app,则不会调用application:didReceiveRemoteNotification:方法

3) 你的app运行在foreground。这时如果来了push message,

Nothing happens on the screen and no sound is played,只会通过delegate来以notification info作为参数来调用application:didReceiveRemoteNotification:方法


另外,在不是锁机,当push message来的时候,

1)若之前app是没有运行的,则执行到application:didFinishLaunchingWithOptions:方法时notification info并不会包含在参数launchOptions里。

2)若之前app有在背景


注意:

目前好像无法通过代码来控制你的app的notification状态的开启和关闭,user只能够在settings > notification里手动设置。而且你也无法在app里获取当然你的app的notificaiton的状态是开启还是关闭。


现在mobile cap app有一个需求是:若user不想每个push message都收到,而是只收取每天下午5点的schedule push message。那么不能够通过disable整个notification,而应该在app设置一个选项来供user选择是否只收5点的push message(by default is NO),当user修改该选项的值时,必须要能上网,因为修改该选项应该send request to server来修改存储在server的对应user的这个选项的值。server在push message时要判断该值



收到push notification时,它会存储在iPhone顶部的notification area。当你再次进入app后,那些 属于该app的notification不会从notification area里删除。怎样才能删除它
解决方法是:在application:didFinishLaunchingWithOptions:方法,application:didReceiveRemoteNotification:方法以及那些把app从后台调到前台trigger的方法( 因为如果不经notification切换app,就不会调用didReceiveRemoteNotification方法)里适当的加上一行代码来 设置badge number=0
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
就会删除notification area里属于该app的所有notifications。

注意,调用了上面那行之后,即使再设置badge number不为0,
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:3];
也会清除所有notifications


其实push notification对于iphone client side来说是很简单的,只需要下面几步:
1. 创建project时注意设置的bundle identifier必须和enable了push notification的App ID的bundle identifier一致
2. 在AppDelegate.m里添加下列代码,即可
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{// Let the device know we want to receive push notifications[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];return YES;
}- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{//add codes to handle receive push notification event
}- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{NSLog(@"My token is: %@", deviceToken);
}- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{NSLog(@"Failed to get token, error: %@", error);
}


APNS feedback service
If a provider attempts to deliver a push notification to an application, but the application no longer exists on the device, the device reports that fact to Apple Push Notification Service. This situation often happens when the user has uninstalled the application. If a device reports failed-delivery attempts for an application, APNs needs some way to inform the provider so that it can refrain from sending notifications to that device. Doing this reduces unnecessary message overhead and improves overall system performance.

For this purpose Apple Push Notification Service includes a feedback service that APNs continually updates with a per-application list of devices for which there were failed-delivery attempts.

http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW3

http://stackoverflow.com/questions/1278834/php-technique-to-query-the-apns-feedback-server


APNS的Quality of Service
APNS包含一个default Quality of Service (QoS) component,它提供store-and-forward message的功能. 当APNs试图push a notification to a device,但这时该device是offline时, QoS就会存储该notification. 当该device再次online时,Qos就会发送存储的notification给它。
对于Qos,要注意2点:
1. 它只能存储一条notification,当有新的notification要存储时,就会覆盖旧的。总之,Qos总是存储最新的那个notification.
2. Qos存储的notification是有expire time (不清楚多久)的,当到时间后就会删除它,这时device online也不会收到它。

ref link:
http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW4



http://www.ngui.cc/zz/2731647.html

相关文章

iOS: #define

typedef与#define的区别。 http://www.sf.org.cn/article/base/200608/18988.html #define 是宏定义,所谓的宏定义,就是将某段代码、地址、字符串等一串的文字,用一个宏来代替。#define是一种预处理。 #define与typedef功能类似&#xff0…

Raphael无法处理event的问题!!!

当你用下列代码画一个矩形,然后click it,照例应该会popup alert var paper Raphael("canvas", 600, 480); var rect paper.rect(10, 40, 50, 50); rect.click(function () {alert(hello); }); 但实际上并不会popup alert,是…

iOS: NSUserDefaults issues

NSUserDefaults用于存储一些app setting。当你关闭app之后再打开后,之前存储的setting依然可以从NSUserDefaults里读取。注意:密码最好不要存储在NSUserDefaults里,要存储也必须用AES/DES加密存储,更安全的方法是 https://github.…

Hibernate: cannot simultaneously fetch multiple bags 问题的解决办法

如果在一个pojo里使用超过一个List的变量,则会出现这个错误 详见: http://www.quanlei.com/2011/10/2817.html 他提供了4个解决方法,我采用方案3: 改变FetchMode为Fetch(FetchMode.SUBSELECT) 其他参考links: http://www.mkyong.…

iOS: UI 开发

中文 http://blog.csdn.net/column/details/zhf156.html Table View http://www.raywenderlich.com/312/beautiful-table-view-helper-class http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html http://cocoawithlove.com/2010/12/uitableview-co…

iOS: login window design

QuickDialog https://github.com/escoz/QuickDialog QQ登录界面demo http://www.cocoachina.com/bbs/read.php?tid69074&page1 Login interface design http://www.photoshop-plus.co.uk/2012/04/13/clean-mobile-login-screen-tutorial/ iPhone Modal View Smalle…

iOS: Classes for fetching and parsing XML or JSON via HTTP

http://cocoawithlove.com/2011/05/classes-for-fetching-and-parsing-xml-or.html

iOS: 2 Xib's with the same File's Owner for 1 IBOutlet object?

http://stackoverflow.com/questions/8978654/2-xibs-with-the-same-files-owner-for-1-iboutlet-object 若2个xib的file owner都指向同一个view controller,那么在2个xib不会同时被load的情况下,分别在不同xib上的控件可以指向view controller里的同一个…

Android: google play graphic assets guide

http://support.google.com/googleplay/android-developer/bin/answer.py?hlen&answer1078870

[iOS] ARC, @property 关键字

https://docs.google.com/document/d/1ZVV5RTzPMUreiFdAMws3jxi2i5Q49iClsGl6JQFwVGg/edit?uspsharing