我正在使用 parse SDK 向 friend 发送推送通知。
注意:我已经使用简单的 REST API 手动测试了推送通知证书,它在那里工作。
使用以下代码:
我在安装表中, channel 是数组类型,我已经匹配了键:
它在解析表中显示删除器,但我的设备没有收到任何类型的通知(请查看随附的屏幕截图)
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)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)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)applicationUIApplication *)application didRegisterForRemoteNotificationsWithDeviceTokenNSData *)newDeviceToken {
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:newDeviceToken];
currentInstallation.channels = @[@“channel”];
[currentInstallation saveInBackground];
}
- (void)applicationUIApplication *)application didFailToRegisterForRemoteNotificationsWithErrorNSError *)error {
if (error.code == 3010) {
//NSLog(@"ush notifications are not supported in the iOS Simulator.");
} else {
// show some alert or otherwise handle the failure to register.
//NSLog(@"application:didFailToRegisterForRemoteNotificationsWithError: %@", error);
}
}
- (void)applicationUIApplication *)application didReceiveRemoteNotificationNSDictionary *)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,我的问题已解决
Best Answer-推荐答案 strong>
如果您的设备运行 iOS 8,请注意 registerForRemoteNotificationTypes 不再受支持。
您需要使用 registerUserNotificationSettings 。这是一个适用于 iOS 7(及以下)和 iOS 8 的示例代码:
- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary*)launchOptions {
// Checks for iOS 8
if ([application respondsToSelectorselector(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 - 使用 Parse SDK for IOS 推送未在设备中交付,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/26650965/
|