here is my code of notification:
private void APP_FOREGROUND_showNotificationMessage(Context context, String title, String message, Intent intent) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "CHANNEL_ID";
String channelName = "CHANNEL NAME";
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(channelId,
channelName,
NotificationManager.IMPORTANCE_HIGH);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
mChannel.setDescription(title);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setSound(Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.offic), attributes); // Here you set the sound
if (manager != null)
manager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.vb_grey)
.setContentTitle(title)
.setColor(Color.RED)
.setSound(Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.offic))
.setContentText(message).setAutoCancel(true).setContentIntent(pendingIntent);
Notification notification = builder.build();
notification.flags = Notification.FLAG_INSISTENT;
manager.notify(0, builder.build());
}
This is my notification code.
I have used .setDefaults(Notification.FLAG_INSISTENT) for looping but notification sound coming one time only.
You can not set the flag by using setDefaults()
You can use the following code:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.vb_grey)
.setContentTitle(title)
.setColor(Color.RED)
.setOngoing(true) //<-- you also need this one
.setSound(Uri.parse("android.resource://"+context+context.getPackageName()+"/"+R.raw.offic))
.setContentText(message).setAutoCancel(true).setContentIntent(pendingIntent);
Notification notification = builder.build();
notification.flags |= Notification.FLAG_INSISTENT;
also check this stackoverflow question
Related
I am working on Android App with Firebase notification service FCM. and I recives notification successfully. but after I recive it is not showin in Android 10 (it work in 9 and 8 and smaller versions).
This is my showing notification code, I dont know what I missed. I check about if there is any changed with 10 but I dont see that...
My method:
private void sendNotification(String messageBody, String title) {
Intent intent;
if(type.equals("order_pending") || type.equals("order_declined")){
intent = new Intent(this, myOrdersActivity.class);
}else { 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);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.logo)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int f =0;
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
// EventBus.getDefault().post(eventObject);
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("isActive", true)) {
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
Intent dialogIntent = new Intent(this, MainActivity.class)
.putExtra("ac", "1");
// sendBroadcast(dialogIntent);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
f =1;
// startActivity(dialogIntent);
}
if(f==0) {
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
Any help please?
and Thanks
The ship has sailed, but:
worked skeleton (Java):
void sendNotif(int icon, String sText) {
String channel_id = "1002";
String sTitle = getString(R.string.app_name);
Intent notificationIntent = new Intent(this, activityMain.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Android 8.0 (API level 26) and higher
// https://developer.android.com/training/notify-user/build-notification
CharSequence name = "statusbar";
String description = getString(R.string.app_name);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(channel_id, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channel_id)
.setSmallIcon(icon)
/*.setContentTitle(sTitle)*/
.setContentText(sText)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(contentIntent)
.setAutoCancel(false);
// notificationId is a unique int for each notification that you must define
// and remember for delete notification
notificationManager.notify(0, builder.build());
}
else {
// Android 7.1 (API level 25) and lower
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(getApplicationContext())
.setTicker(sTitle)
.setContentTitle(sTitle)
.setContentText(sText)
.setSmallIcon(icon)
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent)
.build();
notification.flags |= Notification.FLAG_NO_CLEAR;
// for BIG icon:
//notification.contentView.setImageViewResource(android.R.id.icon, R.drawable.icon);
mNotificationManager.notify(0, notification);
}
}
I simply call a Notification . But It continuously playing sound unless I drag notification ..
Below is my code and right now I am testing on below OREO..
NotificationManager mNotificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "droidudes.zcode";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String name = "easyTouch";
String Description = "Best Utility Tool";
int importance = NotificationManager.IMPORTANCE_HIGH;//IMPORTANCE_HIGH
NotificationChannel mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern();
mChannel.setShowBadge(false);
mNotificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext, NOTIFICATION_CHANNEL_ID)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
///.setVibrate(longArrayOf(0, 1000, 500, 1000))
.setSmallIcon(R.drawable.icon_of_app)
.setContentTitle(mContext.getString(R.string.app_name))
.setContentText("Toucher Hiding Here")
.setAutoCancel(true);
PendingIntent pendingIntent = PendingIntent.getService(mContext, 1,
new Intent(mContext,EasyTouchService.class)
.setAction(EasyTouchService.ACTION_SHOW), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
Notification mNotification = mBuilder.build();
mNotification.flags |= Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
//mNotification.tickerText = getString(R.string.app_name);
// setUpAsForeground(message);
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
I have tried multiple ways by commenting vibration sound in above code . But still sound play for unlimited time..
Try this out:
final PendingIntent pendingIntent = PendingIntent.getActivity(
mCtx,
notfication_id,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(mCtx);
Notification mNotif = builder.setSmallIcon(R.mipmap.logo)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentTitle("title of your notification")
.setContentText("details of your notification")
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(),R.drawable.logo_full))
.build();
mNotif.flags = Notification.FLAG_AUTO_CANCEL;
mNotif.defaults |= Notification.DEFAULT_SOUND;
mNotif.defaults |= Notification.DEFAULT_VIBRATE;
NotificationManager notificationManager = (NotificationManager)mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), mNotif);
I use this code to show notifications users of my app.
On some devices, this code works perfectly, but some devices do not show a notification at all.
Is someone know what is the problem?
private void sendNotification(String title, String messageBody, Map<String, String> allData) {
Intent intent = new Intent(this, ReferralPage.class);
intent.putExtra("title", title);
intent.putExtra("text", messageBody);
String tid = allData.get("tid");
intent.putExtra("tid", tid);
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, "CH_"+tid)
.setSmallIcon(com.escodes.R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_MAX);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
long time = new Date().getTime();
String tmpStr = String.valueOf(time);
String last4Str = tmpStr.substring(tmpStr.length() - 5);
int notificationId = Integer.valueOf(last4Str);
notificationManager.notify(notificationId, notificationBuilder.build());
}
You need to add a notification channel for API 26 and above, like this:
if (Build.VERSION.SDK_INT >= 26) {
String CHANNEL_ID = "ID_OF_THE_CHANNEL";
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
"name_of_the_channel",
NotificationManager.IMPORTANCE_DEFAULT);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
Notification notification = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
.setContentTitle("title")
.setContentText("text")
...
...
.build();
}
I am developing an app in which I am not getting notification when the app is not running in Android Oreo. When a push notification is received, the onMessageReceived method is getting called, but the notification is not showing in the notification bar. Whereas if the app is running (foreground or background) I get a notification in notification bar. public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
RemoteMessage.Notification notification = remoteMessage.getNotification();
Intent intent = new Intent(this, ApplyAndInitiateMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "100";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this,channelId)
.setLargeIcon(bitmap)
.setSmallIcon(R.image_new)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setWhen(remoteMessage.getSentTime())
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_HIGH)
.addAction(0,"Apply LEAVE",pendingIntent)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap).setSummaryText(message).bigLargeIcon(null));
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Channel human readable title", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());
}
}
Create your notification channel before creating notification:
String channelId = "100";
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Channel human readable title", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setLargeIcon(bitmap)
.setSmallIcon(R.image_new)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setWhen(remoteMessage.getSentTime())
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_HIGH)
.addAction(0,"Apply LEAVE",pendingIntent)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap).setSummaryText(message).bigLargeIcon(null));
I have this function for notification generation.I have updated the code as per oreo requirements as well. What could be the issue here. Any help is appreciated.It is working on all the other phones.I have rechecked the settings for notifications as well
private void sendNotification(String messageTitle,String messageBody) {
android.net.Uri sound;
Intent intent = new Intent(getActivity(), MainActivity.class);
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0 /* request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);
long[] pattern = {500, 500, 500, 500, 500};
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
SharedPreferences demoprefs = getActivity().getSharedPreferences("demo", MODE_PRIVATE);
demiIsLoggedIn = demoprefs.getBoolean("demo", true);
if (!demiIsLoggedIn)
sound = defaultSoundUri;
else
sound = null;
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getActivity())
.setSmallIcon(getActivity().getApplicationInfo().icon)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setVibrate(pattern)
.setLights(Color.BLUE, 1, 1)
.setSound(sound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String CHANNEL_ID = "my_channel_01";// The id of the channel.
CharSequence name = "notification channel";// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(12 /* ID of notification */, notificationBuilder.build());
Log.d("wajdan","oneplus");
}
Try to set NotificationChannel in notificationBuilder
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getActivity().getApplicationContext(), "my_channel_01")
...