向用户发送推送:
PFQuery * pushQuery = [PFInstallation query];
PFUser * userReceivingPush;
[pushQuery whereKey:@"owner" equalTo:userReceivingPush];
NSString * alert = [NSString stringWithFormat:@"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 {
}
}];
在应用未运行时响应:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
. . .
// Extract the notification data
NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
// Create a pointer to the Photo object
NSString *photoId = [notificationPayload objectForKey:@"p"];
PFObject *targetPhoto = [PFObject objectWithoutDataWithClassName:@"Photo" objectId:photoId];
// Fetch photo object
[targetPhoto fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
// Show photo view controller
if (!error) {
PhotoVC *viewController = [[PhotoVC alloc] initWithPhoto:object];
[self.navController pushViewController:viewController animated:YES];
}
}];
}
如果您的应用在收到通知时已经在运行,则数据在应用程序中可用:didReceiveRemoteNotification:fetchCompletionHandler: 方法通过 userInfo 字典:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
// Create empty photo object
NSString *photoId = [userInfo objectForKey:@"p"];
PFObject *targetPhoto = [PFObject objectWithoutDataWithClassName:@"Photo" objectId:photoId];
// Fetch photo object
[targetPhoto fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
// Show photo view controller
if (error) {
handler(UIBackgroundFetchResultFailed);
} else if ([PFUser currentUser]) {
PhotoVC *viewController = [[PhotoVC alloc] initWithPhoto:object];
[self.navController pushViewController:viewController animated:YES];
handler(UIBackgroundFetchResultNewData);
} else {
handler(UIBackgroundModeNoData);
}
}];
}
在此处阅读更多信息:https://parse.com/docs/ios/guide
又一推:
NSDictionary *data = @{
@"alert" : @"The Mets scored! The game is now tied 1-1!",
@"badge" : @"Increment",
@"sounds" : @"cheering.caf"
};
PFPush *push = [[PFPush alloc] init];
[push setChannels:@[ @"Mets" ]];
[push setData:data];
[push sendPushInBackground];
推送选项:
自定义通知
如果您想发送的不仅仅是一条消息,您将需要使用 NSDictionary 来打包所有数据。有一些保留字段具有特殊含义。
**alert**: the notification's message.
**badge**: (iOS/OS X only) the value indicated in the top right corner of the app icon. This can be set to a value or to Increment in order to increment the current value by 1.
**sound**: (iOS/OS X only) the name of a sound file in the application bundle.
content-available: (iOS only) If you are a writing a Newsstand app, or an app using the Remote Notification Background Mode introduced in iOS7 (a.k.a. "Background Push"), set this value to 1 to trigger a background download.
**category**: (iOS only) the identifier of th UIUserNotificationCategory for this push notification.
**uri**: (Android only) an optional field that contains a URI. When the notification is opened, an Activity associate with opening the URI is launched.
**title**: (Android, Windows 8, and Windows Phone 8 only) the value displayed in the Android system tray or Windows toast notification.