Reputation: 656
I am getting notification from fcm. But the issue is when i tap on notification message. It contains message body.When i try to put extra's into intent
and try to getExtras()
from intent
i am always getting null from intent
.
Below is my code. Can somebody help me to resolve this issue? Thanks in advance.
VisualogyxMessagingService.java
//This is service of firebase where i received message.
public class VisualogyxMessagingService extends FirebaseMessagingService {
private static final String TAG = VisualogyxMessagingService.class.getSimpleName();
private NotificationUtils notificationUtils;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
handleNotification(remoteMessage.getNotification().getBody());
// this method is called when i tap on notification if notification contains only message.
}
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString().replace("\\n", " "));
HashMap<String, String> hashMap = new HashMap<>(remoteMessage.getData());
handleDataMessage(hashMap);
// this method is called when i tap on notification if notification contains only message with data payload.
}
}
private void handleNotification(String message) {
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
// app is in foreground, broadcast the push message
Intent notificationIntent = new Intent(Config.PUSH_NOTIFICATION);
notificationIntent.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(notificationIntent);
}
else{
// If the app is in background, firebase itself handles the notification
// app is in background, show the notification in notification tray
Intent resultIntent = new Intent(getApplicationContext(), DemoActivity.class);
resultIntent.putExtra("message", message);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
showNotificationMessage(getApplicationContext(), "Visualogyx App Demo", message, contentIntent);
}
}
private void handleDataMessage(HashMap<String, String> hashMap) {
Log.e(TAG, "push json: " + hashMap.toString());
String message = "";
if (hashMap.containsKey("message"))
message = hashMap.get("message");
else if (hashMap.containsKey("default"))
message = hashMap.get("default");
Log.e(TAG, "message: " + message);
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
// app is in foreground, broadcast the push message
Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
pushNotification.putExtra("json", hashMap);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
} else {
// app is in background, show the notification in notification tray
Intent resultIntent = new Intent(getApplicationContext(), DemoActivity.class);
resultIntent.putExtra("json", hashMap);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
showNotificationMessage(getApplicationContext(), "Visualogyx App Demo", message, contentIntent);
}
}
/**
* Showing notification with text only
*/
private void showNotificationMessage(Context context, String title, String message, PendingIntent intent) {
notificationUtils = new NotificationUtils(context);
notificationUtils.showNotification(title, message, intent);
}
}
NotificationUtils.java
public void showNotification(String title, String message, PendingIntent resultPendingIntent) {
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_visualogyx_023";
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(mContext, notification);
r.play();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationChannel.setImportance(NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(notification, Notification.AUDIO_ATTRIBUTES_DEFAULT);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSound(notification)
.setContentIntent(resultPendingIntent)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message).setBigContentTitle(title))
.setTicker("Hearty365")
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher_round));
notificationManager.notify(1337,notificationBuilder.build());
}
DemoActivity.java
//Activity where i try to get extras from intent
. onCreate()
method i tried to fetch intent
extras
public class DemoActivity extends AppCompatActivity {
private static final String TAG = DemoActivity.class.getSimpleName();
private BroadcastReceiver mRegistrationBroadcastReceiver;
private TextView txtRegId, txtMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
txtRegId = (TextView) findViewById(R.id.txt_reg_id);
txtMessage = (TextView) findViewById(R.id.txt_push_message);
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
displayFirebaseRegId();
} else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
// new push notification is received
/*HashMap<String ,String> json = (HashMap<String, String>) intent.getSerializableExtra("json");
String message = "";
String url = "";
HashMap<String,String> jsonObject = new HashMap<>(json);
message = jsonObject.get("message");
if(jsonObject.containsKey("url")){
url = jsonObject.get("url");
Log.e(TAG, "onReceive url: "+url);
}*/
String message = intent.getStringExtra("message");
Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();
txtMessage.setText(message);
}
}
};
displayFirebaseRegId();
Intent intent = getIntent();
String extraAddress = intent.getStringExtra("message");
Toast.makeText(this, "got message:"+extraAddress, Toast.LENGTH_SHORT).show();
}
// Fetches reg id from shared preferences
// and displays on the screen
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();
// register GCM registration complete receiver
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Config.REGISTRATION_COMPLETE));
// register new push message receiver
// by doing this, the activity will be notified each time a new message arrives
LocalBroadcastManager.getInstance(this).
registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Config.PUSH_NOTIFICATION));
// clear the notification area when the app is opened
NotificationUtils.clearNotifications(getApplicationContext());
}
@Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
super.onPause();
}
}
I am always getting null
from intent
when i tap on notification.
Thanks.
Upvotes: 1
Views: 598
Reputation: 569
In VisualogyxMessagingService.java
you first check
if (remoteMessage.getNotification() != null)
and send a notification via handleNotification(remoteMessage.getNotification().getBody());
which in this case puts resultIntent.putExtra("message", message);
into your Intent
which is then fired to DemoActivity.java
.
However, you do not return
immediately after, causing your code to continue to check if (remoteMessage.getData().size() > 0)
, which also sends another intent resultIntent.putExtra("json", hashMap);
, with the "json"
field, which supersedes your previous notification send (since it's the same notification channel & ID). Thus, this new Intent
only has a "json"
field and not "message"
field. To fix this, try putting a return
after the first if
check like so:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
handleNotification(remoteMessage.getNotification().getBody());
return;
// this method is called when i tap on notification if notification contains only message.
}
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString().replace("\\n", " "));
HashMap<String, String> hashMap = new HashMap<>(remoteMessage.getData());
handleDataMessage(hashMap);
}
}
and in your DemoActivity.java
Intent intent = getIntent();
HashMap<String, String> jsonData = intent.getSerializableExtra("json");
String message = intent.getStringExtra("message");
Upvotes: 1
Reputation: 5720
You are adding "json" & "message" both in intent.
try to call intent.hasExtra("json")
or intent.hasExtra("message")
Upvotes: 0