【问题标题】:How do I receive a push notification to one app from another?如何从另一个应用程序接收推送通知?
【发布时间】:2015-08-27 23:16:15
【问题描述】:

我已按照快速入门指南进行解析: 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 后端时的样子:

所以可以肯定地说保存对象很好,但我需要这个过程:

  1. App A 将 PFObject 保存到 Parse 后端(我已经这样做了)
  2. Parse 后端向 App B 发送推送通知(我如何自动执行此操作?)
  3. 应用 B 收到推送通知,说明保存的内容(我该怎么做?)

有没有办法做到这一点?我试过云代码,但它真的很混乱。我确实尝试过设置它,比如 6 次 :( 我知道如何设置推送通知,但我不知道如何解析以在 App A 保存新的PFObject 时自动发送推送通知。

我只需要在我的 Parse 后端收到新的 PFObject 时收到通知,

【问题讨论】:

    标签: ios objective-c parse-platform


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      向用户发送推送:

      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.
      

      【讨论】:

      • 哪里写着“所有者”我要改成用户名?
      • 看你的安装表,如果你没有安装表,那就做一个,然后决定最适合你的路线
      • 我已经从应用程序 A 推送到应用程序 B,但我没有听到声音或振动。我怎样才能得到这些?
      • 你必须在你的包中包含一个声音文件并引用它,你应该在另一个问题中问这个问题
      • 我在哪里添加引用以便在收到推送时声音消失?
      猜你喜欢
      • 2016-11-22
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      • 2015-03-16
      相关资源
      最近更新 更多