Android notification vibration not working - java

I am trying to set notification but something gone wrong when I set vibration for my notification.
Even though it still shows notification there is no vibration.
I have already added user-permission, android.permission.VIBRATE in manifest
here is the code,
class MainActivity : AppCompatActivity() {
lateinit var notificationManager: NotificationManager
lateinit var notificationChannel: NotificationChannel
lateinit var builder: Notification.Builder
val channelId = "com.example.notificationtest"
val descr = "My notification"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
init()
}
fun init() {
val show = findViewById<Button>(R.id.show)
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
show.setOnClickListener {
val intent = Intent(applicationContext, MainActivity::class.java)
val pendingIntent =
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel =
NotificationChannel(channelId, descr, NotificationManager.IMPORTANCE_HIGH)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.RED
notificationChannel.enableVibration(true)
notificationManager.createNotificationChannel(notificationChannel)
builder = Notification.Builder(this, channelId)
.setContentTitle("Android")
.setContentText("new message")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent)
.setCategory(NotificationCompat.CATEGORY_ALARM)
}
notificationManager.notify(0, builder.build())
}
}}

Please use the following code snippet for Android Notification with Vibration setup.
NotificationManager notificationManager;
NotificationCompat.Builder mBuilder = null;
String channelId = "com.example.notificationtest";
String descr = "My notification";
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel(channelId, descr, importance);
notificationChannel.enableLights(true);
notificationChannel.enableVibration(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);
mBuilder = buildNotification(this, "new Message", "Android", null, R.drawable.home, channelId);
mBuilder.setChannelId(channelId);
} else {
mBuilder = new NotificationCompat.Builder(this, channelId);
mBuilder.setSmallIcon(R.drawable.home);
mBuilder.setContentTitle("Android")
.setContentText("new Message")
.setAutoCancel(false)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
}
notificationManager.notify(0, mBuilder.build());
And create a new method like,
#TargetApi(Build.VERSION_CODES.O)
public NotificationCompat.Builder buildNotification(Context mContext, String text, String fromnumber, PendingIntent pendingIntent, int icon, String channelId) {
return new NotificationCompat.Builder(mContext, channelId)
.setSmallIcon(icon)
.setContentTitle(fromnumber)
.setSubText(mContext.getString(R.string.app_name))
.setContentText(text)
.setAutoCancel(true)
.setOngoing(true)
.setAutoCancel(true);
}

Related

firebase push notification sound not working on android app in android studio

I have implement Push notification firebase services class and add on my project and send notification API Request through so notification proper receive but notification sound work to app active condition only but it will be require all notification receive time.
any one this type condition face and resolve please suggest changes.
String title = Objects.requireNonNull(remoteMessage.getNotification()).getTitle();
String text = remoteMessage.getNotification().getBody();
remoteMessage.getMessageType();
// String channelId = remoteMessage.getNotification().getChannelId();
// Log.d("channelId", "onMessageReceived: "+channelId);
final String CHANNEL_ID = "HEADS_UP_NOTIFICATION";
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
"Heads Up notification",
NotificationManager.IMPORTANCE_HIGH
);
getSystemService(NotificationManager.class).createNotificationChannel(channel);
Notification.Builder notification = new Notification.Builder(this,CHANNEL_ID)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.demo)
.setAutoCancel(true)
.setVibrate(new long[] { 0,100,300,500,800,1000, 1000, 1000, 1000, 1000 })
.setOngoing(true)
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_SOUND)
.setVisibility(Notification.VISIBILITY_PUBLIC);
// custom notification
/* MediaPlayer mp;
mp = MediaPlayer.create(PushNotificationServices.this, R.raw.short_notification_sound);
mp.start();*/
#SuppressLint("UnspecifiedImmutableFlag") PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
notification.setContentIntent(contentIntent);
NotificationManagerCompat.from(this).notify(1,notification.build());
super.onMessageReceived(remoteMessage);
Try to use setSound method instead of setDefaults.
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notification: Notification.Builder = Builder(this, CHANNEL_ID)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.demo)
.setAutoCancel(true)
.setVibrate(longArrayOf(0, 100, 300, 500, 800, 1000, 1000, 1000, 1000, 1000))
.setOngoing(true)
.setPriority(Notification.PRIORITY_MAX)
--> .setSound(defaultSoundUri)
.setVisibility(Notification.VISIBILITY_PUBLIC)
First You have uninstall your app then try again.
If this does not work for then try using below code
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);
String channelId = CommonUtill.random();
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.logo)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(messageBody).setBigContentTitle(getString(R.string.app_name)))
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/" + R.raw.notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel mChannel = new NotificationChannel(channelId,
getString(R.string.app_name),
NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
mChannel.setDescription("desc");
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setSound(sound, attributes); // This is IMPORTANT
if (notificationManager != null)
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(943/* ID of notification */, notificationBuilder.build());
This is reference link
Android Push Notification Sound is missing (Not Working ) background and foreground on android

how to display the notification bar?

My program did not show the notificaion bar in android compilesdk28. My code is below. How can I solve this and how do I display the notification in All mobiles?
final String url = userDetailsItem.getIp();
System.out.println("url..."+url);
if (Remaindays .equals("53"))
System.out.println("url..."+url);
{
Context context = null;
String CHANNEL_ID = createNotificationChannel(context);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,CHANNEL_ID)
.setSmallIcon(R.drawable.lesr)
.setContentTitle("my app")
.setContentText(""+url)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(Remaindays+"Days "))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
}
public static String createNotificationChannel(Context context) {
// NotificationChannels are required for Notifications on O (API 26) and above.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// The id of the channel.
String CHANNEL_ID = "Channel_id";
// The user-visible name of the channel.
CharSequence channelName = "Application_name";
// The user-visible description of the channel.
String channelDescription = "Application_name Alert";
int channelImportance = NotificationManager.IMPORTANCE_DEFAULT;
boolean channelEnableVibrate = true;
// int channelLockscreenVisibility = Notification.;
// Initializes NotificationChannel.
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, channelName, channelImportance);
notificationChannel.setDescription(channelDescription);
notificationChannel.enableVibration(channelEnableVibrate);
// notificationChannel.setLockscreenVisibility(channelLockscreenVisibility);
// Adds NotificationChannel to system. Attempting to create an existing notification
// channel with its original values performs no operation, so it's safe to perform the
// below sequence.
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);
return CHANNEL_ID;
} else {
// Returns null for pre-O (26) devices.
return null;
}
}
API => 28 requires Notification Channel
https://developer.android.com/reference/android/app/NotificationChannel
if you want video links
https://www.youtube.com/watch?v=tTbd1Mfi-Sk
Template:
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(getApplicationContext(), DummychatActivty.class);
intent.putExtra("user_id", Config.to_user_id);
intent.putExtra("profile_img_Url", Config.profile_img_Url);
intent.putExtra("user_name", Config.to_user_name);
intent.putExtra("user_image_url", Config.to_user_image_url);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String CHAT_NOTIFICATION_CHANNEL_ID = "101";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
#SuppressLint("WrongConstant") NotificationChannel Chat_notificationChannel = new NotificationChannel(CHAT_NOTIFICATION_CHANNEL_ID, "Chat Notifications", NotificationManager.IMPORTANCE_MAX);
//Configure Notification Channel
// notificationChannel.setDescription("General Notifications");
Chat_notificationChannel.setLightColor(Color.RED);
Chat_notificationChannel.enableLights(true);
Chat_notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
Chat_notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(Chat_notificationChannel);
}
NotificationCompat.Builder chat_notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHAT_NOTIFICATION_CHANNEL_ID)
.setVibrate(new long[]{0, 100})
.setPriority(Notification.PRIORITY_MAX)
.setLights(Color.RED, 3000, 3000)
.setAutoCancel(true)
.setContentTitle(Config.fcm_headline)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setColor(getResources().getColor(R.color.colorPrimary))
.setSmallIcon(R.drawable.push)
.setLargeIcon(bitmap)
.setGroup("CHAT")
.setContentText(Config.content)
.setAutoCancel(true)
.setSound(defaultSound);
chat_push_count = chat_push_count + 1;
notificationManager.notify(chat_push_count, chat_notificationBuilder.build());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
chat_notificationBuilder.setChannelId("101");
}
private void sendNotification(Notification nobj) {
Intent intent = new Intent(context, NotificationActivity.class);
intent.putExtra("Notification", "click");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(context, defaultSoundUri);
r.play();
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(Html.fromHtml(nobj.title))
.setContentText(Html.fromHtml(nobj.desc))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setChannelId("my_channel_01")
.setContentIntent(pendingIntent);
generateNotification(notificationBuilder);
}
Please check this you can get help , its working code in my live project
public void generateNotification(NotificationCompat.Builder notificationBuilder) {
NotificationManager notificationManager =
(NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
String CHANNEL_ID = "my_channel_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = context.getString(R.string.app_name);
// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name,
importance);
// Create a notification and set the notification channel.
notificationManager.createNotificationChannel(mChannel);
}
PreferenceHelper preferenceHelper = PreferenceHelper.getInstance(context);
preferenceHelper.setNotificationId(preferenceHelper.getNotificationId() + 1);
notificationManager.notify(preferenceHelper.getNotificationId(),
notificationBuilder.build());
}

o android notification sound is not playing

This is my code for making notification and notification is showing but not playing sound please help me to identify mistakes in my code and do we need any permission to play sound,vibrate for notification?
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String id = "my_channel_01";
CharSequence name = "oreiomilla";
String description ="i love me";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel .setSound(alarmSound,attributes);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotificationManager.createNotificationChannel(mChannel);
int notifyID = 1;
String CHANNEL_ID = "my_channel_01";
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("New Message")
.setContentText("You've received new messages. "+ct)
.setSmallIcon(R.drawable.ic_app_icon)
.setChannelId(CHANNEL_ID) .setTicker("Showing button notification") //
.addAction(android.R.drawable.ic_dialog_alert, "Visit", pIntent) // accept notification button
.addAction(android.R.drawable.ic_dialog_email, "ignore", pIntent)
.build();
mNotificationManager.notify(notifyID, notification);
To set a sound to notifications in Oreo, you must set sound on NotificationChannel and not on Notification Builder itself. You can do this as follows
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.notification_mp3);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel("YOUR_CHANNEL_ID",
"YOUR CHANNEL NAME",
NotificationManager.IMPORTANCE_DEFAULT)
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID,
context.getString(R.string.app_name),
NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
mChannel.setDescription(msg);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setSound(sound, attributes); // This is IMPORTANT
if (mNotificationManager != null)
mNotificationManager.createNotificationChannel(mChannel);
}
Try this it will work for you.
private void BigTextNotificationForDays(Context context, String Message, String Author) {
Intent resultIntent = new Intent(context, SplashActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) Calendar.getInstance().getTimeInMillis(), resultIntent, PendingIntent.FLAG_ONE_SHOT);
//To set large icon in notification
// Bitmap icon1 = BitmapFactory.decodeResource(getResources(), R.drawable.big_image);
//Assign inbox style notification
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(Message);
bigText.setBigContentTitle(context.getString(R.string.app_name));
bigText.setSummaryText(Author);
//build notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context,createNotificationChannel(context))
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(Message)
.setStyle(bigText)
.setLargeIcon(icon)
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setVibrate(new long[]{1000, 1000})
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
//.setOngoing(true);
// Gets an instance of the LocalNotificationManager service
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//to post your notification to the notification bar
mNotificationManager.notify(3730, mBuilder.build()); // (int) System.currentTimeMillis() set instead of id. if at a time more notification require
}
public static String createNotificationChannel(Context context) {
// NotificationChannels are required for Notifications on O (API 26) and above.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// The id of the channel.
String channelId = "Channel_id";
// The user-visible name of the channel.
CharSequence channelName = "Application_name";
// The user-visible description of the channel.
String channelDescription = "Application_name Alert";
int channelImportance = NotificationManager.IMPORTANCE_DEFAULT;
boolean channelEnableVibrate = true;
// int channelLockscreenVisibility = Notification.;
// Initializes NotificationChannel.
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, channelImportance);
notificationChannel.setDescription(channelDescription);
notificationChannel.enableVibration(channelEnableVibrate);
// notificationChannel.setLockscreenVisibility(channelLockscreenVisibility);
// Adds NotificationChannel to system. Attempting to create an existing notification
// channel with its original values performs no operation, so it's safe to perform the
// below sequence.
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);
return channelId;
} else {
// Returns null for pre-O (26) devices.
return null;
}
}

Why startForeground not working? Notifications

I have a code for Notifications in JobService:
public class MyJobService extends JobService {
String NOTIFICATION_CHANNEL_ID = "Nilesh_channel";
#Override
public boolean onStartJob(JobParameters params) {
Intent medication = new Intent(this, Medication.class);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
mNotificationManager.createNotificationChannel(notificationChannel);
}
// to diaplay notification in DND Mode
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
channel.canBypassDnd();
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setContentTitle(Medication.medication.get(i).getDrug())
.setContentText("Время приема " + Medication.medication.get(i).getTime())
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.logonotification)
.setAutoCancel(true);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(medication);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
notificationBuilder.setContentIntent(resultPendingIntent);
startForeground(1000, notificationBuilder.build());
mNotificationManager.notify(1000, notificationBuilder.build());
Toast.makeText(this, "Запуск сервиса", Toast.LENGTH_SHORT).show();
}
But line startForeground(1000, notificationBuilder.build()) not working, I have not see my notification in notification bar. If I deleting this line, notification is shown, but it is not foreground.
In Manifest - <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
How can I fix it?

Android Firebase Notification if Application is opened

I have configured the FCM for my 3° application android but in this case the notification is received only if application is closed or background, when app is running nothing appears.
This is my .FirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
private void sendNotification(String title, String body) {
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
Intent showFullQuoteIntent = new Intent(this, Home.class);
showFullQuoteIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// both of these approaches now work: FLAG_CANCEL, FLAG_UPDATE; the uniqueInt may be the real solution.
//PendingIntent pendingIntent = PendingIntent.getActivity(this, uniqueInt, showFullQuoteIntent, PendingIntent.FLAG_CANCEL_CURRENT);
int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent pendingIntent = PendingIntent.getActivity(this, uniqueInt, showFullQuoteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification notification = new NotificationCompat.Builder(getApplicationContext(), "")
.setSmallIcon(R.drawable.logo)
.setContentTitle(title)
.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.logo))
.setColor(getResources().getColor(R.color.colorPrimary))
.setAutoCancel(false)
.setSound(notificationSound)
.setContentIntent(pendingIntent)
.setContentText(body)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(m, notification);
}
}
Called in:
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
Please can you help me? FCM is linked to my firebase account and i send notification from firebase site...
FCM Log:
D/FA: Logging event (FE): notification_foreground(_nf), Bundle[{firebase_event_origin(_o)=fcm, firebase_screen_class(_sc)=Home, firebase_screen_id(_si)=-8508869816157962301, message_device_time(_ndt)=0, message_name(_nmn)=test, message_time(_nmt)=1521054559, message_id(_nmid)=5215133478455582605}]
I have use some code like:
Notification not showing in Android 8 Oreo
And Guidelines from Google: https://developer.android.com/training/notify-user/build-notification.html
But if application is opened notification not received but logged in console..
I have solved with this code:
private void sendNotification(String title, String body) {
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String CHANNEL_ID = "my_channesl_01";
CharSequence name = "NEWS";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel = null;
mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mNotificationManager.createNotificationChannel(mChannel);
Intent showFullQuoteIntent = new Intent(this, Home.class);
showFullQuoteIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent pendingIntent = PendingIntent.getActivity(this, uniqueInt, showFullQuoteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification notification = new NotificationCompat.Builder(this.getApplicationContext(), "")
.setSmallIcon(R.drawable.logo)
.setContentTitle(title)
.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.logo))
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setAutoCancel(false)
.setSound(notificationSound)
.setContentIntent(pendingIntent)
.setContentText(body)
.setChannelId(CHANNEL_ID)
.build();
mNotificationManager.notify(m, notification);
}
else
{
Intent showFullQuoteIntent = new Intent(this, Home.class);
showFullQuoteIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent pendingIntent = PendingIntent.getActivity(this, uniqueInt, showFullQuoteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification notification = new NotificationCompat.Builder(getApplicationContext(), "")
.setSmallIcon(R.drawable.logo)
.setContentTitle(title)
.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.logo))
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setAutoCancel(false)
.setSound(notificationSound)
.setContentIntent(pendingIntent)
.setContentText(body)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(m, notification);
}
}

Categories

Resources