我正在使用parse SDK向 friend 发送推送通知。 注意:我已经使用简单的 REST API 手动测试了推送通知证书,它在那里工作。
使用下面的代码: 我在安装表中, channel 是数组类型,其中我匹配了键: 它在解析表中显示 delever,但我的设备没有收到任何类型的通知(检查随附的屏幕截图)
PFQuery * pushQuery = [PFInstallation query];
[pushQuery whereKey:@"channels" containedIn:array];
NSString * alert = [NSString stringWithFormat:@"You have a new message from %@!", [PFUser currentUser].username];
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
alert, @"alert",
@"default", @"sound",
@"Increment", @"badge",
nil]; [PFPush sendPushDataToQueryInBackground:pushQuery withData:data block:^(BOOL succeeded, NSError *error) {
if (!error) {
}
else {
} }];
我的委托(delegate)设置:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Uncomment and fill in with your Parse credentials:
[Parse setApplicationId:@“MY APP ID
clientKey:@“MY CLIENT KEY”];
// Override point for customization after application launch.
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken {
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:newDeviceToken];
currentInstallation.channels = @[@“channel”];
[currentInstallation saveInBackground];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
if (error.code == 3010) {
//NSLog(@"Push notifications are not supported in the iOS Simulator.");
} else {
// show some alert or otherwise handle the failure to register.
//NSLog(@"application:didFailToRegisterForRemoteNotificationsWithError: %@", error);
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
}
//NOTE: after login i change the channels value in installation table by
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
// [currentInstallation setDeviceTokenFromData:newDeviceToken];
NSString *string=[NSString stringWithFormat:@"channel%@",[PFUser currentUser]];
NSArray *array=[NSArray arrayWithObjects:string, nil];
currentInstallation.channels =array;
[currentInstallation saveInBackground];
更新:使用适用于 IOS 8 的新 Parse SDK,我的问题已解决
最佳答案
如果您的设备运行 iOS 8,请注意,不再支持 registerForRemoteNotificationTypes
。
您需要使用registerUserNotificationSettings
。下面是一个示例代码,应该可以在 iOS 7(及以下版本)和 iOS 8 上运行:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
// Checks for iOS 8
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
} else {
[application registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
// The rest of your didFinishLaunchingWithOptions: method should be here
return YES;
}
编辑:我刚刚看到您说它可以与 REST API 一起使用,因此这不会导致您的问题。不过,将您的代码更新为此是一件好事,因为您可能会遇到 iOS 8 设备的问题。
编辑2:
问题可能来自您的 Parse 项目配置。您需要手动启用“客户端推送通知”。默认情况下,Parse 会禁用它,这会阻止 Parse SDK 发送推送通知。
转至 Parse.com,进入应用程序的“设置”,在“推送”部分,然后设置“客户端推送已启用?”到"is"。
关于ios - 使用适用于 IOS 的 Parse SDK 进行推送未在设备中交付,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26650965/