In Android L Preview doc,
https://developer.android.com/preview/notifications.html
there is a mention of "Cloud-synced notifications - act on a notification on your Android tablet and it is also dismissed on your phone." but no detailed information on it.
Any idea where can I find more information about this and how to implement this new feature in my app ?
This can be achieved with User Notifications, which is a feature of Google Cloud Messaging.
The idea is that your server registers a group of devices (identified by their GCM registration ID) that belong to the same user and gets a single notification ID for that user. Then you can send a GCM message to all the devices of that user using that single notification ID. This message can be sent either from your server to all devices of the user or from one device to the other devices of that user.
When one device handles the notification, you can send a GCM message to all the other devices belonging to the same user, to let them know the notification was viewed on one device, and the other devices would receive that message and dismiss the notification.
To send an upstream (device-to-cloud) message, you must use the GoogleCloudMessaging API. Specifying a notification_key as the target for an upstream message allows a user on one device to send a message to other devices in the notification group—for example, to dismiss a notification. Here is an example that shows targeting a notification_key:
GoogleCloudMessaging gcm = GoogleCloudMessaging.get(context);
String to = NOTIFICATION_KEY;
AtomicInteger msgId = new AtomicInteger();
String id = Integer.toString(msgId.incrementAndGet());
Bundle data = new Bundle();
data.putString("hello", "world");
gcm.send(to, id, data);
Related
I want to make an app where one user can ask for help and all other using this app will get that notification. I am using Parse.com for this as server. i am totally new to this please help me.I got registered my device by key etc. now i can send push notification from parse.com to my device.
but don't know how to make this on my device to send this to other devices?
You can create subscribe devices to different unique channel names, then you can send notification containing required payload to the device using the channel to which other device is subscribed.
//subscribing to channel
ParsePush.subscribeInBackground("Channel_Name");
//getting installed devices
ParseQuery pushQuery = ParseInstallation.getQuery();
//query for getting devices with channel name
pushQuery.whereEqualTo("channels", "Channel_Name");
ParsePush push = new ParsePush();
push.setQuery(pushQuery);
push.setMessage("Your message payload");
push.sendInBackground();
You can subscribe multiple devices to same channel, but in this case when notification is send , it will be sent to all devices subscribed to a particular channel.
For more info: https://parse.com/docs/android/guide#push-notifications
my question is the same as this one:
android device specific push notifications by using azure mobile services
But I'm using .Net backend. This is the part where I send the notification:
Dictionary<string, string> data = new Dictionary<string, string>() { { "message", "this is the message" } };
GooglePushMessage message = new GooglePushMessage(data, TimeSpan.FromHours(1));
Services.Push.SendAsync(message);
but there is no way to pass in the registration ID.
UPDATE
I've also tried using the payload property of GooglePushMessage:
GooglePushMessage message = new GooglePushMessage();
message.JsonPayload = JsonConvert.SerializeObject(new { registration_id = "blablabla", data = new { message = "77" } });
It turns out that it is ignoring the registration_id property, because I'm still getting the notification on my device.
What I want to achieve is to get API calls from third parties and use the registration ids that I have stored in my DB to send notifications to specific devices.
Mobile Services uses Notification Hubs to handle it's push notifications. Notification Hubs filters push (i.e. does targeted push to specific devices, users, etc) using a Tag system. So if you want to be able to push to a specific Android Registration ID, when the device registers for Push Notifications with your Mobile Service, you should specify tags that you want to tie your registration to like so:
ToDoActivity.mClient.getPush().register(gcmRegistrationId, "tag1", "tag2");
If you want to push based off of the registration ID, you'd use that ID as one of your tags. Then from your .NET backend, when you call SendAsync, you can specify a tag (or tag expression) to target a specific device. So our call to push becomes:
Services.Push.SendAsync(message, registrationID);
The above text was incorrect. Tags are limited to 120 characters (https://msdn.microsoft.com/en-us/library/dn530749.aspx) and the GCM registration ID is too long for this. TODAY you're stuck with using an alternate tag that is less than 120 characters. If you have a UserID that is known on the device you could use that as a tag and then from your backend you can push to that specific User ID. Alternatively, you could generate a GUID on app launch and use that as a tag and then from your backend push to that GUID whenever you wanted to reach the specific device.
I'm new to Android, I'm developing an application which is a little bit similar to WhatsApp, I have different users, and I want when a User send a message, other users should receive a Push Notification.
So when a user add a new message, when a value will be inserted to the class, all the users should receive Push notifications.
I know how to send a push notification from the console, but I want push notifications to be sent automatically.
Thanks
You can send push notifications with parse by using channels whereby each user subscribes to a set of channels and you can send pushes to particular channels.
OR
You can use advanced targeting in which you use the installation class to select the devices.
ParseQuery<ParseInstallation> pq = ParseInstallation.getQuery();
pq.whereEqualTo("area", message3);
pq.whereEqualTo("group", message2);
pq.whereNotEqualTo("key", value);
// Send push notification to query
ParsePush push = new ParsePush();
push.setQuery(pq); // Set our Installation query
push.setMessage("New Message");
push.sendInBackground();
Intent in = new Intent(Post.this,Notifications.class);
progress.dismiss();
startActivity(in);
OR
You can use cloud code to receive the data sent by the user and send push notifications to relevant devices
I am started to exploring on GCM. Please clarify the following things.
By Registering with GCM server using the SenderID (Project ID get it from google API console), the Different device will giving unique registration id. I have used the following code to send message from server to all the devices by adding registration id as deviceid in the devicelist . I got the registration id of device by logging in the logcat.
Sender sender = new Sender("MY_API_KEY");
Message message = new Message.Builder().collapseKey("1")
.timeToLive(3)
.delayWhileIdle(true)
.addData("message",
"this text will be seen in notification bar!!")
.build();
MulticastResult result;
ArrayList<String> devicesList = new ArrayList<String>();
devicesList.add(deviceid1);
devicesList.add(deviceid2);
result = sender.send(message, devicesList, 1);
Is it right way to send message like above mentioned?
If that's the case how can design the code it will work after release the particular application? how can i get the registration id of all the devices after releasing?
Your server needs to keep track of all the registration ids from the devices. Therefore, you need a database table to store those registration ids. Then you need to expose a web service, or a HTTP POST entry point for your clients to upload their registration ids.
After a device successfully register with Google GCM server, you will receive the registration id in the onRegistered() callback method in GCMBaseIntentService. This is where you want to upload the registration id to your server.
I am playing around with the Google Cloud Messaging sample code given in Android SDK. Both the client and server code works fine. I have installed the GCM client android app in three android devices and when the server is running each of the devices gets registered successfully to the GCM Server. And whenever I try to send message the server broadcasts the messages to all the three devices. But my question is whether we can blacklist any of the device or allow the server to send message to any of the particular device.
Is there any method to restrict the device using the registrationID generated in the server for each devices?
Here is the server code for sending push notification
if (devices.size() == 1) {
String registrationId = devices.get(0);
if(!(registrationId .equalsIgnoreCase("APA91bE61KYmI4Qzn9NKkh6wkHr1ya8FIfGbc_gYJf0_33558IeFnj9j8j4EPRaJeSKJ_CK6_a9FQLt_CapLE4e_mvRaq3hytJalfjTBbkAxbLIdfdXyoxatSrDPmtxSdyaeiRV-Bav-4qOSlURH5mL69Fa9ktX8JFoow")))
{
Message message = new Message.Builder().build();
Result result = sender.send(message, registrationId, 5);
results = Arrays.asList(result);
}
} else {
// send a multicast message using JSON
Message message = new Message.Builder().build();
MulticastResult result = sender.send(message, devices, 5);
results = result.getResults();
}
There is no built-in method to restrict some devices in GCM-Server but you can create your own blackList and send message to the devices other than this list.
You can create your devicesList which does not include registrationIds in the blackList and send multicast message to this list.
Check javadoc for more information about the GCM server-side code here.