I have problem with my notification. In my app, I use Firebase. I call notification every get message. But I have problem:
- Notification does not update Title and Content Text.
- Pending Intent does not open the target Activity
This my Notification Code:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData().get("message"));
try {
JSONObject json = new JSONObject(remoteMessage.getData());
int action = json.getInt(Sample.ACTION);
if(action==1){
String order = json.getString(Sample.ORDER);
sendNotification(order);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
}
private void sendNotification(String order) {
int requestID = (int) System.currentTimeMillis();
Bundle args= new Bundle();
args.putString(Sample.ORDER, order);
Intent intent = new Intent(this, ProgressOrderActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtras(args);
this.startActivity(intent);
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestID,intent, PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.fishtank_bubbles);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText("NEW ORDER. GET NOW !!!")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
Notification noti = notificationBuilder.build();
noti.flags = Notification.FLAG_INSISTENT;
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
}
}
please how to solve it , or any mistakes from my code ?