【发布时间】:2017-06-07 08:28:00
【问题描述】:
在 iPhone/iPad 上安装应用后,应用可以第一时间收到通知。一旦应用程序处于非活动状态,它就会停止在前台和后台收到通知。
有人可以指出,我错过了什么。自从我第一次收到以来,消息格式似乎没有问题。
服务器代码:
message.put("priority", "high");
message.put("content_available",true);
if (to != null)
{
message.put("to", to.replace("\\", ""));
}
if (messageId != null)
{
message.put("message_id", messageId);
}
JSONObject subobj = new JSONObject();
subobj.put("sound", "default");
message.put("notification", subobj);
message.put("data", payload);
if (timeToLive != null)
{
message.put("time_to_live", timeToLive);
}
if (delayWhileIdle != null && delayWhileIdle)
{
message.put("delay_while_idle", true);
}
if (collapseKey != null)
{
message.put("collapse_key", collapseKey);
}
message.put("delivery_receipt_requested", true);
客户代码:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
// First run Delete keychain
if (![[NSUserDefaults standardUserDefaults] objectForKey:@"FirstRun"]) {
// Delete values from keychain here
application.applicationIconBadgeNumber = 0;
[self resetKeychain];
[[NSUserDefaults standardUserDefaults] setValue:@"1strun" forKey:@"FirstRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
self.viewController = [[MainViewController alloc] init];
CGRect screenBounds = [[UIScreen mainScreen] bounds];
#if __has_feature(objc_arc)
self.window = [[UIWindow alloc] initWithFrame:screenBounds];
#else
self.window = [[[UIWindow alloc] initWithFrame:screenBounds]
autorelease];
#endif
self.window.autoresizesSubviews = YES;
//notification
[self updateDurationLabel];
UIFont *font = [UIFont boldSystemFontOfSize:10.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
[self.segFromStyle setTitleTextAttributes:attributes forState:UIControlStateNormal];
[self.segToStyle setTitleTextAttributes:attributes forState:UIControlStateNormal];
[self flowManager];
UILocalNotification *remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotification) {
application.applicationIconBadgeNumber = 0;
self.launchNotification = remoteNotification.userInfo;
NSLog(@"NotificationCheck: remoteNotification");
}
UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) {
application.applicationIconBadgeNumber = 0;
NSLog(@"NotificationCheck: localNotification");
self.launchNotification = localNotification.userInfo;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:localNotification.userInfo options:0 error:nil];
NSString* jsonString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
NSLog(@"Dict:%@", jsonString);
}
[self.window makeKeyAndVisible];
return YES;
}
【问题讨论】:
标签: google-cloud-messaging apple-push-notifications