iOS: Some issues about push notification

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"我设置错了

iOS: Some issues about push notification_第1张图片

上图中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


你可能感兴趣的:(iOS: Some issues about push notification)