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:方法
解锁后,会以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:方法。
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时要判断该值
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];就会删除notification area里属于该app的所有notifications。
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:3];也会清除所有notifications
- (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); }