I have problem in custom notification in android - 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.

Related

Android Notification with pending URL-Intent - only by clicking the notification

I'm trying to create a pending URL-Intent for a Notification, that is called, if the user clicks the notification.
The problem is that the Intent is being called by creating the notification, so the url is getting called in the web browser immediatly. I want to prevent this, but do not know how?
Here is my code
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.com"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(MyApp.getContext(), 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MyApp.getContext(), "MyApp")
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("my App")
.setContentText("Click here to call the URL")
.setStyle(new NotificationCompat.BigTextStyle().bigText("...."))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
;
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(MyApp.getContext());
notificationManager.notify(getNotificationId(), builder.build());
How can I prevent executing the intent after the "notify" call??
Intent should only be executed, when the user clicks on the Notification.
try this:
public void notification()
{
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse("https://www.google.com/"));
#SuppressLint("WrongConstant") PendingIntent pending =
PendingIntent.getActivity(this, 0, notificationIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
String channelId = getString(R.string.app_name);
NotificationChannel notificationChannel = null;
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(getApplicationContext());
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel(channelId, channelId,
NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription(channelId);
notificationChannel.setSound(null, null);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new
NotificationCompat.Builder(getApplicationContext(), channelId)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("my App")
.setContentText("Click here to call the URL")
.setStyle(new NotificationCompat.BigTextStyle().bigText("...."))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pending)
.setAutoCancel(true)
;
notificationManager.notify(0, builder.build());
}

Notification auto disappear after showing

public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
intent = new Intent(context, AlertDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); // added new line
intent.putExtra("STARTED_BY_RECEIVER", true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
String CHANNEL_ID = "channel id";// The id of the channel.
CharSequence name = "Channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
Notification notification = new Notification.Builder(context)
.setContentIntent(pendingIntent)
.setContentText("Some Text")
.setContentTitle("Notification")
.setAutoCancel(true)
.setOngoing(true)
.setSmallIcon(R.drawable.ic_notification_praying)
.setChannelId(CHANNEL_ID)
.build();
notificationManager.createNotificationChannel(mChannel);
notificationManager.notify(100 , notification);
} else {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
intent = new Intent(context, AlertDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); // added new line
intent.putExtra("STARTED_BY_RECEIVER", true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification_praying)
.setContentIntent(pendingIntent)
.setContentText("Some Text")
.setContentTitle("Notification")
.setAutoCancel(true)
.setOngoing(true);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
notificationManager.notify(100, builder.build());
}
}
I've made an application which show notification on time that user chose in main activity and Notification should disappear only when user tap on notification otherwise not..and it was working fine unless I've updated my phone to android Oreo (8.0) now notification show at time but auto disappear after sometime.. How to make notification not auto disappear (in android Oreo) but only when user tap on it?
alright I figure this out.. it was battery optimize option which was killing my application so that's why notification was disappearing.. I removed my application from auto optimize now it's working fine.

Notification not showing up on Android 8

I've read most question/answers on this but my notification still isn't showing up.
My Broadcast receiver gets called, but then nothing happens. There is no error message nor exception. here is my code:
public class AlarmReceiver extends BroadcastReceiver {
private static int notificationId = 0;
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 101, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context, "mychannel")
.setSmallIcon(R.drawable.icon_awake)
.setContentTitle("MyAlarm")
.setContentText("Something")
.setAutoCancel(false)
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent);
notificationManager.notify(notificationId++, mNotifyBuilder.build());
}
}
I think you forget to Read Google Documents on Oreo 8.0.
If you want to create a notification on 8.0 you need to use NotificationChannel
like this:
NotificationChannel channel = null;
String chanel = "MY_Musixbox";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_LOW;
channel = new NotificationChannel(chanel, "MusicBox", importance);
notificationManager.createNotificationChannel(channel);
}
And then add your notification
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context, "mychannel")
.setSmallIcon(R.drawable.icon_awake)
.setContentTitle("MyAlarm")
.setContentText("Something")
.setChannelId(chanel);
.setAutoCancel(false)
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent);
notificationManager.notify(notificationId++, mNotifyBuilder.build());
Okay, so I will answer this myself in case somebody finds it: seems you need to manually create the notification channel yourself too:
if (Build.VERSION.SDK_INT >= 26) {
NotificationChannel channel = new NotificationChannel("mychannel",
"Channel name",
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Channel description");
notificationManager.createNotificationChannel(channel);
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 101, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context, "mychannel")
.setSmallIcon(R.drawable.icon_awake)
.setContentTitle("MyAlarm")
.setContentText("Something")
.setAutoCancel(false)
.setWhen(System.currentTimeMillis())
.setChannelId("default_channel")
.setContentIntent(pendingIntent);
notificationManager.notify(notificationId++, mNotifyBuilder.build());
Try this. you have to define channel id. thats compulsory in android 8.0

Java Notification with a PendingIntent and putExtra don't work

I have a FireBase Message. If it comes in, I show a Notification (works). When I click on the Notification it should open my application (works). Now I want to use putExtra(), to hand over some information to my app (that works only sometimes - if the app is in the foreground). That's my problem.
private void sendMyNotification(String message) {
int rndNo4Intent = Math.round((float)(Math.random()*100000000));
int rndNo4Msg = Math.round((float)(Math.random()*100000000));
//On click of notification it redirect to this Activity
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("action","openWiki"+rndNo4Msg); // My Test-Data
PendingIntent pendingIntent = PendingIntent.getActivity(this, rndNo4Intent , intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String channelId = getString(R.string.default_notification_channel_id);
Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("CodingSpace")
.setContentText(message)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
// Since android Oreo notification channel is needed. Sonst kommt nichts an.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"CodingSpaceChannel",
NotificationManager.IMPORTANCE_HIGH); // in Oreo ist High so, dass es einen Ton gibt
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(rndNo4Msg, notificationBuilder.build());
}
In the .MainActivity-Class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Intent iin= getIntent();
String msg = iin.getStringExtra("action");
Toast.makeText(MainActivity.this,"get: "+ msg,Toast.LENGTH_SHORT).show();

Handle notifications from Firebase 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

Categories

Resources