hi i am trying to display the push notification using BroadcastReceiver it works fine when the app is minimized but when the app is closed i can not display the push notification.
and my question is what is possible techniques or solution to display the push notification when the app is closed?
bellow is code for notification using broadcast receiver
public class Alarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String message=intent.getStringExtra("message");
String title=intent.getStringExtra("title");
String click_action=intent.getStringExtra("click_action");
notification(context,message,title,click_action,intent);
}
private void notification(Context context, String message, String title, String click_action, Intent intent) {
Toast.makeText(context, title + message + click_action, Toast.LENGTH_SHORT).show();
if (click_action.equals("Time_LineActivity")) {
intent = new Intent(context, Time_LineActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
} else if (click_action.equals("MainActivity")) {
intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
} else {
intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
}
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification.Builder notification = new Notification.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification.build());
}
}
and bellow is my FirebaseMessagingService code
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getData().get("title" +" "+"body" +" "+"click_action"));
String title=remoteMessage.getData().get("title");
String message=remoteMessage.getData().get("body");
String click_action=remoteMessage.getData().get("click_action");
sendNotification(title,message,click_action);
}
private void sendNotification(String title, String message, String click_action) {
Intent broadcastedIntent=new Intent(this, Alarm.class);
broadcastedIntent.putExtra("message", message);
broadcastedIntent.putExtra("title", title);
broadcastedIntent.putExtra("click_action", click_action);
sendBroadcast(broadcastedIntent);
}
}
Thanks in advance..