Windows Azure notification hub registration ID and token location? - android

I am using Windows Azure notification hub to send notifications to the users in my Android application. I have some kind of a chat in my application, and I would like to send notifications only to specific users. I suppose that I have to specify their tags when sending notifications. Where are those tags stored? How am I sure that the user is properly registered?
I have this code for registering users:
#SuppressWarnings("unchecked")
private void registerWithNotificationHubs() {
new AsyncTask() {
#Override
protected Object doInBackground(Object... params) {
try {
String regid = gcm.register(SENDER_ID);
hub.register(regid, "tag");
} catch (Exception e) {
return e;
}
return null;
}
}.execute(null, null, null);
}
When not using a tag, the notifications work and they are sent to all the devices. I'd like to add a tag and send the notification to specific tag.

We call it tags not tokens. You can specify one ore more tags for each device registration and then use those tags to rout messages to particular user or group. Documentation id here.

Related

FirebaseMessagingService Intercept notifications when app closed

can i intercept notifications when my app is closed?
I need for set badge with this library ShortcutBadger
Thanks.
There are 3 types of notifications:
notification: Can be send from the web console or any backend, it has predefines values. If the app is open the behaviour is customizable on onMessageRecieve if the app is closed triggers a default notification.
data: a key value pair, only Strings. Can be send from any backend. The behaviour is always defined in onMessageReceived method.
notification and data: Combination of previous it will have the behaviour of a notification, the data will be available as extras once the notification is clicked in the default launcher activity. Can be send from the web console or any backend.
A push is a json called Payload which contains those objects:
payload: {
data: {...}
}
Yes, you can send yourself a data type notification it will always do what you write in the onMessageReceived method inside the MessagingService.
This doc should help you
https://firebase.google.com/docs/cloud-messaging/concept-options?hl=es-419
If you dont have a server use Functions.
Since the default notification wont be shown, you will probably want to show your own.
If you want to also show a notification then the NotificationCompat class must be called from inside onMessageReceived. The visual notification is not related to the push message, in fact, a visual notification can be triggered by pressing a button.
For creating a visual notification, the best approach is to let Android Studio do it for you. Second click on the packages where your activities .java are, new, then selecet ui-component and there is the notification. It will create a basic template of a notification. Then use those methods inside onMessaReceived passing the info that has to be show to the user.
The docs about the class
https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html
And you will probably find this error
NotificationCompat.Builder deprecated in Android O
In case you never solved this, the problem is not how you are implementing it within your app, but how the JSON data payload is being sent. See this question and the respective answers for why you are not receiving the messages while they are in the background.
Very short summary is, if you are receiving the notification payload, it will never trigger in the background. If you receive the data payload without notification, you can parse and perform actions while the app is in the background.
do you mean it?
public class AppFcmMessagingsService extends FirebaseMessagingService {
private static final String TAG = "FirebaseMessageService";
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
try {
if(remoteMessage.getData().size() > 0) {
final JSONObject jsonObject = new JSONObject(remoteMessage.getData().toString());
Log.d(TAG,"remoteMessage = " + jsonObject.toString());
int badgeCount = 1;
ShortcutBadger.applyCount(getApplicationContext(), badgeCount);
}
} catch (Exception e) {
Log.e(TAG, "onMessageReceived: ", e);
}
if(remoteMessage.getNotification() != null) {
int badgeCount = 1;
ShortcutBadger.applyCount(getApplicationContext(), badgeCount);
Log.d(TAG, "notification body : " + remoteMessage.getNotification().getBody());
}
}
}

Will push notifications through GCM be supported in eclipse

I am using eclipse IDE for development. I followed lots of tutorials and implemented push notifications in my project but I'm not getting the GCM Registration id, So my questions is: will eclipse support the push notifications or not?
Below is the code snippet; if you need more clarity please comment below.
registerReceiver(mHandleMessageReceiver, new IntentFilter(DISPLAY_MESSAGE_ACTION));
Log.v("testing", "testing");
// Get GCM registration id
final String regId = GCMRegistrar.getRegistrationId(this);
Log.v("regId", regId);
// Check if regid already presents
if (regId.equals("")) {
// Registration is not present, register now with GCM
Log.v("registerd","am here");
GCMRegistrar.register(this, SENDER_ID);
} else {
// Device is already registered on GCM
if (GCMRegistrar.isRegisteredOnServer(this)) {
// Skips registration.
Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
} else {
// Try to register again, but not in the UI thread.
// It's also necessary to cancel the thread onDestroy(),
// hence the use of AsyncTask instead of a raw thread.
final Context context = this;
Log.v("hello","am here");
mRegisterTask = new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
// Register on our server
// On server creates a new user
ServerUtilities.register(context, name, email, regId);
return null;
}
#Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
Push notifications will work no matter what IDE you're using, but I have two pieces of advice for you:
You need to migrate your project to Android Studio for better support.
Your GCM registration library GCMRegistrar is too old. You need to get the latest version, checkout here.
Hope this helped :)
Finally got the answer by using following tutorial.. link

Parse - Notification not being received

I am trying to send a Parse Push Notification from one Android application to all others.
The following is the set-up code in my Application object:
Parse.enableLocalDatastore(this);
ParseObject.registerSubclass(Game.class);
Parse.initialize(this, "code", "code");
ParsePush.subscribeInBackground(ParseHelper.SUBSCRIPTION_CHANNEL_GAME);
The following is the Push Notification code:
ParsePush push = new ParsePush();
String message = "Hello";
push.setChannel(ParseHelper.SUBSCRIPTION_CHANNEL_GAME);
push.setMessage(message);
push.sendInBackground(new SendCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(CreateGameActivity.this, "Success", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(CreateGameActivity.this, "Failed", Toast.LENGTH_LONG).show();
}
}
});
break;
}
Even though the Success Toast is called, I still don't see the Notification appearing on any of the two Android devices I have installed the app on.
I have tested the Push Notifications via www.parse.com's Dashboard and that does work. Why won't it work in my app though?
To send notifications from a device, you have to do one extra step. Go into the settings of your app on parse and enable Client Push. That should resolve your issue.

Possible to have multiple receiver ids in GoogleCloudMessaging?

I am following this tutorial:
http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/
my app is getting gcm_regid, that is stored in mysql. With this id, I can send message via php to chosen regid;
but when i deinstall the application - and reinstall, it get's a new reg_id - thats ok too, this is stored in mysql too.
But when I send a message to the first gcm_regid (this was it's previous gcm_regid) the phone is getting that message too, even it has got a new gcm_regid (due to reinstall) - why is that so, and how can I delete the previous id?
and where is that id? in my device? stored in gcm server?
and a second question:
do I have to take majour changes when I change GCMRegistrar in the tutorial above to Google Cloud Messaging - what I have to do there
When GCM assigns a new Registration ID to your device, the previous IDs that were assigned to it continue to work. If you send a message to an older Registration ID assigned to your device, the message will be delivered, but your server will get a response containing a canonical Regsitration ID, which is the newest Registration ID of that device. If you get such a response, you should delete the old Registration ID from your DB.
As for your second question, you don't have to make major changes in order to stop using GCMRegigtrar and use GoogleCloudMessaging instead. In fact, your code should look simpler after you make that change.
You'll have one call to GoogleCloudMessaging.register inside an AsyncTask in one of your activities. This method has to be called in an AsyncTask because it's blocking, so it can't be executed on the main thread. The good thing about it is that you get the response in the same place you call the method, and don't have to wait for it in the GCMBroadCastReceiver/IntentService.
Your GCMBroadcastReceiver/IntentService will have to handle just incoming messages. They won't have to deal with registration anymore.
Here's a partial example of the new registration process, taken from the official GCM Demo :
/**
* Registers the application with GCM servers asynchronously.
* <p>
* Stores the registration ID and the app versionCode in the application's
* shared preferences.
*/
private void registerInBackground() {
new AsyncTask<Void, Void, String>() {
#Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID);
msg = "Device registered, registration ID=" + regid;
// You should send the registration ID to your server over HTTP, so it
// can use GCM/HTTP or CCS to send messages to your app.
sendRegistrationIdToBackend();
// For this demo: we don't need to send it because the device will send
// upstream messages to a server that echo back the message using the
// 'from' address in the message.
// Persist the regID - no need to register again.
storeRegistrationId(context, regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
// If there is an error, don't just keep trying to register.
// Require the user to click a button again, or perform
// exponential back-off.
}
return msg;
}
#Override
protected void onPostExecute(String msg) {
mDisplay.append(msg + "\n");
}
}.execute(null, null, null);
}

Android, Google Cloud Messaging - push gets to phone, but does not create a push notification on the device

I have been trying to get Google Cloud Messaging to create push notifications. I am able to register the device, store the registration id on the server, and then send the push message back to the device.
I am able to do all these and the catLog showed that the push message got to the device. But on the device the push notification message did not appear.
What could be the cause of this?
Just in case, here is how I register the device:
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals(""))
{
// Automatically registers application on startup.
GCMRegistrar.register(getApplicationContext(), SENDER_ID);
}
else
{
// Device is already registered on GCM, check server.
if (GCMRegistrar.isRegisteredOnServer(getApplicationContext()))
{
// Not sure what to do here :)
}
else
{
if ( user_id != null )
{
GCMRegistrar.register(this, SENDER_ID); // google register
setRegistrationId ( user_id , regId ); // saves id on server
}
}
}
End on onPostExecute I do GCMRegistrar.setRegisteredOnServer(getApplicationContext(), true);
Here is my onMessage() method in my GCMIntentService
#Override
protected void onMessage(Context ctxt, Intent message) {
Bundle extras=message.getExtras();
for (String key : extras.keySet()) {
Log.d(getClass().getSimpleName(),
String.format("onMessage: %s=%s", key,
extras.getString(key)));
}
}
Is this the problem maybe? How should it actually be?
Thank you!
What could be the cause of this?
Here are some possibilities:
You do not have a GCMIntentService subclass in your app.
You do, but it is not registered in the manifest.
You do, but you did not override onMessage()
You did, but your onMessage() is not properly logging the event or otherwise letting you know that the event occurred.

Categories

Resources