android firebase reg id recived but notification not show in phone display - android

I will try to show personal single notification on my phone tray, but I can't rich, so help.
I am having an issue with FireBase Cloud Messaging in which I get the Token from the device and send the notification test through the Google Firebase notification console, however, the notification is never logged nor pushed to the android virtual device. The documentation for FCM is almost exactly the code that I have below and little else in the way of what else you would have to do to get push notifications working with firebase. I have gone through all of the setup information (build.gradle additions, Installing google play services, etc...) as specified in the documentation, but still do not have messages generating. What is wrong with the code that I am not receiving my push notifications to the logcat or the device? Please let me know any further information that would be helpful. Thanks.
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);
displayFirebaseRegId();
} else if (intent.getAction().equals(Config.PUSH_NOTIFICATION))
{
String message = intent.getStringExtra("message");
Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();
txtMessage.setText(message);
}
}
};
displayFirebaseRegId();
}
private void displayFirebaseRegId() {
SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0);
String regId = pref.getString("regId", null);
Log.e(TAG, "Firebase reg id: " + regId);
if (!TextUtils.isEmpty(regId))
txtRegId.setText("Firebase Reg Id: " + regId);
else
txtRegId.setText("Firebase Reg Id is not received yet!");
}
#Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Config.REGISTRATION_COMPLETE));
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Config.PUSH_NOTIFICATION));
NotificationUtils.clearNotifications(getApplicationContext());
}
#Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
super.onPause();
}
And I will add lib of fire base messaging is:
compile 'com.google.firebase:firebase-messaging:11.0.4'

You don't need to subscribe inside the BroadcastReceiver you can just do it inside the onTokenRefresh method in the FirebaseInstanceIdService
You don't need to get the push notification in the BroadcastReceiver, you have to do it inside the onMessageReceive in the FirebaseMessagingService
FCM is extremely unreliable with emulators, simply use a real device, I have struggled with this and in some cases I even get the notification days later when opening the emulator for other projects, test this with real phones

Related

Unable to receive push notification from fcm in android

I am trying to send push notification from FCM .But unable to receive it in my android device.
At first you can see in the image that it's not getting initialized but later the initialization is successful.
I am able to get the FCM registration id but unable to get the push message .
here is the screenshot:
OnReceive Method :
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// checking for type intent filter
if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
// gcm successfully registered
// now subscribe to `global` topic to receive app wide notifications
FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);
displayFirebaseRegId();
} else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
// new push notification is received
String message = intent.getStringExtra("message");
Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();
txtMessage.setText(message);
}
}
};
I am using this example of AndroidHive and I have also tried to test it on different devices with different versions.
if anyone can give some idea then it will be very helpful to me.

Pushing notification in background

I want to know how to push notification with out letting user know that a push notification is received.Just my application should be aware of the notification and do the task required depended upon notification.
public class GCMIntentService extends GCMBaseIntentService
{
#Override
protected void onMessage(Context context, Intent intent)
{
Log.i(TAG, "Received message");
if (intent.getExtras().containsKey("payload"))
{
String message = intent.getExtras().getString("payload");
CommonUtilities.displayMessage(context, message);
// notifies user
//generateNotification(context, message);
}
}
}
here in this method dont notify user when the message comes.
EDIT:
this answer is applicable only if you have used GCM directly in your app implemented by yourself.
If you are using parse.com for this then you cant achieve what you have asked in question. because parse API internally notifies this user.

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.

Notification received but not showing

I'm adding Urban Airship push notifications to my Android app. My custom BroadcastReceiver is receiving the notifications when I send a test push. Here's the onReceive() method.
public class IntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent == null)
return;
String action = intent.getAction();
if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) {
int id = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, 0); //Breakpoint here
Log.w("my app", "Received push notification. Alert: "
+ intent.getStringExtra(PushManager.EXTRA_ALERT)
+ " [NotificationID="+id+"]");
logPushExtras(intent);
} else if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) {
//Other stuff here
}
Execution stops at the breakpoint (position shown in the comment) and the details are logged.
No notification is shown in the notification area though. In my other app with UA push notifications, I think this was all I did, but it doesn't seem to be working this time.
I suspect I've done something wrong in my manifest file or that I've forgotten to implement a class somewhere.
Any ideas?
Extra Info
Extended Application:
public class ExtendedApplication extends Application {
public void onCreate(){
AirshipConfigOptions options = AirshipConfigOptions.loadDefaultOptions(this);
UAirship.takeOff(this, options);
PushManager.enablePush();
PushPreferences prefs = PushManager.shared().getPreferences();
Log.e("my app", "My Application onCreate - App APID: " + prefs.getPushId());
PushManager.shared().setIntentReceiver(IntentReceiver.class);
}
}
Edit
I've tried adding
PushManager.shared().setNotificationBuilder(new BasicPushNotificationBuilder());
to my ExtendedApplication as suggested in this post.
I was sending a test push with a blank "Alert" field from the UA site. I tried with some text in there and now it works. I don't know why, but I don't think that's something that should fail silently.
Moral of the story: Don't leave your Alert field blank.

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