When I send notification and app does not open already,I cant see my notification on listview in mainactivity.When I send notification and app already is open,I see on listview.How can I see notifications on listview everytime I send notification?
my service
private void sendNotification(String messageBody) {
SharedPreferences a=getSharedPreferences("noti",MODE_PRIVATE);
SharedPreferences.Editor editor=a.edit();
editor.putInt("i",a.getInt("i",0)+1);
editor.putString(String.valueOf(a.getInt("i",0)+1),messageBody);
editor.apply();
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.farmer)
.setContentTitle("Zengin Çiftçi")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
my mainactivity
a=getSharedPreferences("noti",MODE_PRIVATE);
for(int i=a.getInt("i",0);i>0;i--){
liste.add(a.getString(String.valueOf(i),""));
}
Related
i have created a cloud function which sends data with the notifications. Now i want to open always the specific activity. Unfortunately its opening always the MainActivity. Here is my Code of my Firebase Service:
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> dataSet = remoteMessage.getData();
if (!dataSet.get("kind").equals("0")) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.baseline_send_24)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("body"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}else{
Intent intent = new Intent(this, SettingsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.baseline_send_24)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("body"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
I dont know if i have to define it somewhere else. Thanks
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction("ACTION.HOME_ACTION");
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
and set this pendingintent in notification
.setContentIntent(pendingIntent)
Incase you are using Firebase for your push notifications. If you use the 'notification' type payload, while sending from server, Firebase manages the display of notification for your app without giving a callback and by default will always open the launcher activity of your app.
You can send extra data from the server side, access that data in your launcher activity getIntent methods and redirect to some other activity as needed
I get a push notification from FCM. When my application is minimized or screen is locked, I see a title of notification : Here is your notification and different icon.
How I can change this? This is my code and how I create and display a notification I don't have this:
private void createNotification( String messageBody) {
Intent intent = new Intent( this , NotificationActivity. class );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Wiadomosc z serwera")
.setContentText(messageBody)
.setAutoCancel( true )
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
}
Modified createNotification method .
1 . Add setLargeIcon() method to set large icon .
2 . Put your own title in setTitle() method .
private void createNotification( String messageBody) {
Intent intent = new Intent( this , NotificationActivity. class );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setSmallIcon(R.mipmap.ic_launcher) /*Icon to display in the Notification Bar */
.setLargeIcon(R.mipmap.largeicon) /*Icon to display when you scroll down notification tray */
.setContentTitle("Your own Title") /*Notification Title which will display when you scroll down Notification tray */
.setContentText(messageBody)
.setAutoCancel( true )
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
}
You'll have to use setLargeIcon(Bitmap bitmap) method.
Bitmap largeIconBitmap = BitmapFactory.decodeResource(this.getResources(),R.drawable.launcher_logo);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setLargeIcon(largeIconBitmap)
.setSmallIcon(R.mipmap.ic_launcher)
I have to send a URL in notification and I want upon clicking the notification, the link will be opened immediately by Chrome. Currently, when I click the notification, it only opens my app.
What changes do I have to implement in this code for my desired behavior?
private void sendNotification(String messageBody, String title) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
I have a notification set up :
private void sendNotificationForCancellation(String appointmentId, String title, String message) {
//AppController.getSharedPreferences().edit().putString(Constants.CALL_CASE_ID, notifObject.getCaseDetails().getCaseID()).commit();
Intent intent = new Intent(this, ActCallRequestsDetail.class);
intent.putExtra("appointment_id", appointmentId);
intent.putExtra("isAppIdAvailable", true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_note_plus)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.app_icon))
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Random generator = new Random();
notificationManager.notify(generator.nextInt(), notificationBuilder.build());
}
On click of notification it opens a activity
Scenario-1:
It is working fine when I have a single notification.
.
Scenario-2:
Assume 2 notifications generated from above notification setup first
notification click, it is opening the activity Now i am there in the
opened activity(ActCallRequestsDetail.class) When I click the
second notification, nothing happens the activity
(ActCallRequestsDetail.class) is not refreshed
.
How to solve the Scenario-2
Solution was just adding a new ID for PendingIntent reference
private void sendNotificationForCancellation(String appointmentId, String title, String message) {
Random generator = new Random();
//AppController.getSharedPreferences().edit().putString(Constants.CALL_CASE_ID, notifObject.getCaseDetails().getCaseID()).commit();
Intent intent = new Intent(this, ActCallRequestsDetail.class);
intent.putExtra("appointment_id", appointmentId);
intent.putExtra("isAppIdAvailable", true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, generator.nextInt(), intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_note_plus)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.app_icon))
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(generator.nextInt(), notificationBuilder.build());
}
This method is inside my service ----->>>>
public class MyFirebaseMessagingService extends FirebaseMessagingService
my target activity (Event) is not opening when i m clicking on notification.
//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(String messageBody) {
Intent intent = new Intent(getApplicationContext(), Events.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.djchc_logo)
.setContentTitle("Notification")
.setContentText(messageBody)
//.setAutoCancel(true)
.setSound(defaultSoundUri);
//.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}