Handle notifications from Firebase Android - android

I tried to learn about Firebase. And I successfully received notification from it. But how I can open another activity when click on the notification?
This my code:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("test")
.setContentText(remoteMessage.getData().get("message"));
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}

You need to build a PendingIntent and notify using NotificationManager
Intent intent = new Intent(this, MyActivity.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.your_icon)
.setContentTitle("Notification Title")
.setContentText(messageBody)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
As Tosin suggested, if you're sending push notification from Firebase console, you need to put your payload into data key

It's sth about notification, nothing to do firebase.
check this you need to Define the Notification's Action

Related

I have problem in custom notification in android

Here is my firebase messaging service code file. My service is not called.
public class NotificationService extends FirebaseMessagingService {
String myTitle, myImage;
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
myTitle = remoteMessage.getData().get("title");
myImage = remoteMessage.getData().get("body");
Log.d("Service","Hi this is service");
//bitmap = getBitmapfromUrl(myImage);
//Toast.makeText(this,"Hi test",Toast.LENGTH_SHORT).show();
showNotification(myTitle,myImage);
}
private void showNotification(String myTitle,String myImage) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("Notification_Title", "yes");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//Toast.makeText(this,title,Toast.LENGTH_SHORT).show();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = "Custom_Notification";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_notifications_none_white_24dp)
.setContentTitle(myTitle)
.setContentText(myImage)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Custom Notification",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());
}
}
If you are not received any remote message or your service is not working mean, check out the StackOverflow link.
To Create Custom Notification Channel
private void showNotification(String myTitle,String myImage) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("Notification_Title", "yes");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = "Custom_Notification";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_notifications_none_white_24dp)
.setContentTitle(myTitle)
.setContentText(myImage)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Custom Notification",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build()); }
Above code is working fine after receiving the remote message. If you want to add any custom layout then use below code
// Get the layouts to use in the custom notification
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);
// Apply the layouts to the notification
Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded)
.build();
Notification custom layout
Note: In android 10, and above Notification Service is not calling basically in the background. You will receive a notification but custom design it will not reflect. Still, If you want to receive notification in the background also. then listen to the broadcast receiver for firebase message event. that time you have to wake up your app to the foreground.

Notification cancel not working

Notification canceling works when i have one device. But when i add one more device with another id, it not cancel any of notifications. I dont understand what i am doing wrong.
Here is some code
private void cancelNotification(RemoteMessage remoteMessage)
{
int id = Integer.parseInt(remoteMessage.getData().get("id"));
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(id);
}
private void sendNotification(RemoteMessage remoteMessage)
{
Intent intent = new Intent(this, Activity.class);
intent.putExtra("url",remoteMessage.getData().get("url"));
PendingIntent pintent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent , 0);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification notificationBuilder = new Notification.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.splash_launcher))
.setSmallIcon(R.drawable.splash_launcher)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("text"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pintent)
.build();
int id = Integer.parseInt(remoteMessage.getData().get("id"));
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, notificationBuilder);
}
There's nothing wrong in the code you've shown. To cancel a notification you've previously sent you have to call the cancel function with the same id that you've used to call the notify function. If your code is not working the RemoteMessage you're giving to your cancelNotification function has a different id than the RemoteMessage you're giving to your sendNotification function.

How to send Notification through Firebase console including Custom data?

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());
}

target activity not opening in android app on click of notification

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());
}

Removing notification after click

I just started working with notifications and now I'm trying to remove the notification and launch the app once the notification has been tapped in the notificationcenter.
I tried to work with the following code:
import android.app.NotificationManager;
public class ExpandNotification {
private int NOTIFICATION = 546;
private NotificationManager mNM;
public void onCreate() {
mNM.cancel(NOTIFICATION);
setContentView(R.layout.activity_on);
//Toast.makeText(this, "stopped service", Toast.LENGTH_SHORT).show();
}
I think this code executes the other class when tapped?
PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, new Intent(this, ExpandNotification.class), 0);
However the notification doesn't go away, nor does the application launch.
But I'm able to swipe it to left or right to remove it but that's not what I want..
To get the same effect using Notification.Builder or NotificationCompat.Builder call setAutoCancel(true) on the Builder instance.
Use the flag Notification.FLAG_AUTO_CANCEL
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
// Cancel the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
and to launch the app:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Create a new intent which will be fired if you click on the notification
Intent intent = new Intent(context, App.class);
// Attach the intent to a pending intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
This answer is too much late but specially i write following solution because notification constructor become deprecated so use notification using builder , like following :
**.setAutoCancel(true)** is used to remove notification on click
and entire notification is like follwoing :
private void makeNotification(String title,String msg){
Intent resultIntent = new Intent(this, MasterActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentIntent(resultPendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setAutoCancel(true)
.setContentText(msg);
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
Calling this method with title and message you get perfect notification.
You can directly add .setAutoCancel(true) this line into your code in order to remove notification on click.
This must be added in your builder variable.
Example:
mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("Notification")
.setContentText("Hello")
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
Best & simple way is set builder.setAutoCancel(true) it's cancel your notification after clicking on notification. I hope this code help you.
Builder for notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.btn_star);
builder.setContentTitle("This is title of notification");
builder.setContentText("This is a notification Text");
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
For open an activity on clicking notification
Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
Show bulder in notification
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(114, builder.build());
Complete code for show a notification with icon, image, title, description, auto cancel and on click open an activity
public void ShowIntentNotification(View v)
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.btn_star);
builder.setContentTitle("This is title of notification");
builder.setContentText("This is a notification Text");
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(114, builder.build());
}
For my it was that
.setPriority(Notification.PRIORITY_HIGH);
that was causing the notification to not clear after click... make sure you use:
.setPriority(Notification.PRIORITY_DEFAULT);
And .setAutoCancel(true) should work.

Categories

Resources