使用Parse push作为独立服务

时间:2014-08-28 08:07:47

标签: parse-platform push

我正在构建iOS和Android应用程序(两个应用程序)。 我正在寻找一个我不再信任的Urban Airship的替代品。主要 因为我看不到任何价格。

我想使用Parse Push作为独立产品。这可能吗? 我不想使用任何其他Parse功能。

1 个答案:

答案 0 :(得分:1)

正如https://parse.com/docs/push_guide#setup/iOS

中的Parse Push通知(iOS版Android)设置中所述

是的,你可以。当然,您需要包含解析sdk,因为您需要通过Parse系统注册设备。所以:

注册设备以接收推送通知(iOS示例)

- (void)application:(UIApplication *)application
        didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    // Store the deviceToken in the current Installation and save it to Parse.
    PFInstallation *deviceInstallation = [PFInstallation currentInstallation];
    [deviceInstallation setDeviceTokenFromData:deviceToken];
    // this will be used later for sending push notifcations to this device       
    [deviceInstallation setChannels:@[deviceToken]]; 
    [deviceInstallation saveInBackground];
}

通过 iOS

发送推送通知
PFPush *pushObj = [[PFPush alloc] init];
NSString* destDeviceToken = @"<DESTIONATION DEVICE TOKEN>";

// Through channels, you can uniquely identify devices or group of them for multicast 
// push notification (imagine that more than one installation on your 
// Parse database keep the same channel name). 
// In this case we have set the channel name for each installation with the 
// only unique deviceToken
[pushObj setChannels:@[destDeviceToken]];

// Push structure message
[pushObj setData:@{@"My Message!!",@"alert"}];

// send push ( so, send to parse server that handle the push notification 
// request and deliver it for you)
[pushObj sendPushInBackground];

通过 Android

发送推送通知
String destDeviceToken = "<DESTIONATION DEVICE TOKEN>";
JSONObject data = new JSONObject("{\"alert\": \"My Message!!\"}");     
ParsePush *push = new ParsePush();
push.setChannel(destDeviceToken);
push.setData(data);
push.sendPushInBackground();

通过 Javascript (云代码)发送推送通知 (它需要在Parse的Cloud代码上实现)

var destDeviceToken = '<DESTIONATION DEVICE TOKEN>';
Parse.Push.send({
  channels: [ destDeviceToken ],
  data: {
    alert: "My Message!!"
  }
}, {
  success: function() {
    // Push was successful
  },
  error: function(error) {
    // Handle error
  }
});

通过 REST 调用发送推送通知 (它允许您执行发送推送通知而无需再处理Parse,因此可以从支持REST调用的任何其他平台执行)

curl -X POST \
  -H "X-Parse-Application-Id: <YOUR PARSE APP ID>" \
  -H "X-Parse-REST-API-Key: <YOUR REST API KEY>" \
  -H "Content-Type: application/json" \
  -d '{
        "channels": [
          "<DESTIONATION DEVICE TOKEN>"
        ],
        "data": {
          "alert": "My Message!!",
        }
      }' \
  https://api.parse.com/1/push

通过这种方式,您不需要任何其他实施,例如注册用户或类似的东西。

希望它有所帮助!