I use Firebase Messaging Service, to receive Push-Notifications. Everything is working very well, if my screen is unlocked.
When my screen is locked, i receive the Notification, but my phone is not making any sound, is not vibrating and my light is not flashing.
This is my code to generate the message:
private void sendNotification(String messageBody, String messageTitle) {
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.mipmap.ic_launcher)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setLights(Color.CYAN, 1, 1)
.setPriority(Notification.PRIORITY_MAX)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
Use this for send notification this work fine
private static final long[] vPattern = new long[]{1000, 1000, 1000, 1000, 1000};
public static void sendNotification(Context ctx, NotificationData notificationData) {
try {
NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Integer notifiId = (int) System.currentTimeMillis();
Intent intent_for_Notification = new Intent();
Bundle b = new Bundle();
b.putSerializable(Constants.NOTIFICATION_DATA, notificationData);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx);
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationData.msg));
mBuilder.setContentText(notificationData.msg);
mBuilder.setContentTitle(ctx.getResources().getString(R.string.app_name));
intent_for_Notification.putExtras(b);
intent_for_Notification.setClass(ctx, SplashActivity.class);
intent_for_Notification.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, notifiId, intent_for_Notification, 0);
mBuilder.setContentIntent(contentIntent);
mBuilder.setSmallIcon(R.mipmap.ic_launcher_196);
mBuilder.setVibrate(vPattern);
mBuilder.setPriority(Notification.PRIORITY_HIGH);
mBuilder.setAutoCancel(true);
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
mNotificationManager.notify(notifiId, mBuilder.build());
} catch (Exception e) {
e.printStackTrace();
}
}
Have you include <uses-permission android:name="android.permission.WAKE_LOCK" />
in your manifest?
One legitimate case for using a wake lock might be a background service that needs to grab a wake lock to keep the CPU running to do work while the screen is off. Again, though, this practice should be minimized because of its impact on battery life. - Android Developers documentation
You may try adding notificationBuilder.setDefault(defaults)
as I do notice some of the phone are not producing any sound even you have set .setSound(defaultSoundUri)
.
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setLights(Color.CYAN, 1, 1)
.setPriority(Notification.PRIORITY_MAX)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent);
//Added defaults
int defaults = 0;
defaults |= android.app.Notification.DEFAULT_SOUND;
defaults |= android.app.Notification.DEFAULT_VIBRATE;
notificationBuilder.setDefaults(defaults);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.