Monday, 2 February 2015

app notification 4 -- channels






https://parse.com/docs/push_guide#sending-channels/iOS



first you can send message to a channel by:


curl -X POST \
-H "X-Parse-Application-Id: xxx" \
-H "X-Parse-REST-API-Key: xxxxx" \
-H "Content-Type: application/json" \
-d '{ "channel": "server1",
"data": { "alert": "hello!" ,"message": "click this-->"} }' \

https://api.parse.com/1/push


Then only device listening to "server1" will get this message.

To listen to a channel,  you can do this by two ways:


1. change channels for devices in : https://www.parse.com/apps/mto-curtains/collections



2. IOS can do like below:


Using Channels

The simplest way to start sending notifications is using channels. This allows you to use a publisher-subscriber model for sending pushes. Devices start by subscribing to one or more channels, and notifications can later be sent to these subscribers. The channels subscribed to by a given Installation are stored in the channels field of the Installation object.

Subscribing to Channels

A channel is identified by a string that starts with a letter and consists of alphanumeric characters, underscores, and dashes. It doesn't need to be explicitly created before it can be used and each Installation can subscribe to any number of channels at a time.
Adding a channel subscription can be done using the addUniqueObject: method in PFObject. For example, in a baseball score app, we could do:
// When users indicate they are Giants fans, we subscribe them to that channel.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation addUniqueObject:@"Giants" forKey:@"channels"];
[currentInstallation saveInBackground];
Once subscribed to the "Giants" channel, your Installation object should have an updated channels field.
Installation_channel
Unsubscribing from a channel is just as easy:
// When users indicate they are no longer Giants fans, we unsubscribe them.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation removeObject:@"Giants" forKey:@"channels"];
[currentInstallation saveInBackground];
The set of subscribed channels is cached in the currentInstallation object:
NSArray *subscribedChannels = [PFInstallation currentInstallation].channels;
If you plan on changing your channels from Cloud Code or the data browser, note that you'll need to call some form of fetch prior to this line in order to get the most recent channels.






No comments:

Post a comment