如何从另一个应用程序收到推送通知?

时间:2015-08-27 23:16:15

标签: ios objective-c parse-platform

我已按照快速入门指南进行解析: https://parse.com/apps/quickstart#parse_data/mobile/ios/native/existing

我使用此代码保存PFObject

PFObject *testObject = [PFObject objectWithClassName:@"TestObject"];
testObject[@"Bookmarks"] = @"Restore Successful";
[testObject saveInBackground];

下面是我的PFObject保存到Parse后端时的样子: enter image description here

所以可以安全地说保存对象很好但是我需要这个过程:

  1. App A将PFObject保存到Parse后端(我已经这样做了)
  2. 解析后端向App B发送推送通知(我如何自动执行此操作?)
  3. 应用B接收推送通知,说明保存了什么(我该怎么做?)
  4. 有没有办法做到这一点?我已经尝试过Cloud Code,但它真的令人困惑。我已经尝试过设置它,例如,6次:(我知道如何设置推送通知但我不知道如何在应用A保存新的{{{}时自动发送推送通知进行解析1}}。

    当我的Parse后端收到新的PFObject

    时,我只需要收到通知

2 个答案:

答案 0 :(得分:1)

你不应该把它视为“一个应用程序到另一个应用程序”,而应该关注,从一种情况到另一种情况,因为这会限制你未来的实现,除非你严格编写管理员或简单的目的,只是为您的项目做最好的事情。 Parse推送目前支持使用相同的Parse.com applicationId&的 6 不同的推送证书。 clientKey。基本上,您需要做的就是为每个应用创建individual push certificate,就像您为第一个应用做的那样,并将其上传到Parse.com设置中的推送证书。这不是特定于设备/操作系统的,并且在将这些添加到后端控制台之后将无法工作。完成后,启用Client Push Enabled设置,然后您只需要定位推送通知。由于您未在问题中包含“用户”作为优先级,因此只需利用参数来定位应用程序名称/ ID。见here。换句话说,定位推送通知以使用安装类中的applicationId列或appName

答案 1 :(得分:0)

向用户发送推送:

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.