我已经去过:
但是,我在Android手机(小米Redmi 5A,牛轧糖)的锁定屏幕上没有看到通知。顺便说一句,我在当地通知。没有推送通知或GCM。
我测试了Gmail应用程序,它运行正常。 Gmail通知显示在锁定屏幕中。
我是否错误地构建了通知?那我该如何建造?
private fun showNotificationWith(message: String) {
val channelId = "com.example.notif.channelId"
val channelName = "App status"
val contentTitle = "Title"
val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_small_exclamation)
.setContentTitle(contentTitle)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel = NotificationChannel(channelId, channelName, importance)
notificationBuilder.setChannelId(channelName)
notificationManager.createNotificationChannel(notificationChannel)
notificationManager.notify(message.hashCode(), notificationBuilder.build())
} else {
notificationManager.notify(message.hashCode(), notificationBuilder.build())
}
}
答案 0 :(得分:1)
尝试一下...
您可以使用NotificationChannel.ie的 lockscreenVisibility
属性,
notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
例如,
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel = NotificationChannel(channelId, channelName, importance)
notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
notificationBuilder.setChannelId(channelName)
notificationManager.createNotificationChannel(notificationChannel)
notificationManager.notify(message.hashCode(), notificationBuilder.build())
} else {
notificationManager.notify(message.hashCode(), notificationBuilder.build())
}
快乐的编码...
答案 1 :(得分:0)
尝试一下:
通知类别
public class NotificationService extends FirebaseMessagingService {
private static final String TAG = NotificationService.class.getSimpleName();
String body = "", dataa = "", title = "", type = "";
boolean notification, sound, vibration;
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
private Vibrator vib;
Ringtone ringtone;
private String Sender="";
private String SenderProfileUrl="";
private boolean isConnected;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "remoteMessage......" + remoteMessage.getData());
try {
Map<String, String> params = remoteMessage.getData();
JSONObject object = new JSONObject(params);
Log.e(TAG, object.toString());
body = object.getString("not_id");
dataa = object.getString("data");
title = object.getString("not_type");
type = object.getString("type");
Sender = object.getString("Sender");
SenderProfileUrl = object.getString("SenderProfileUrl");
wakeUpScreen();
addNotification(remoteMessage.getData());
}
/* when your phone is locked screen wakeup method*/
private void wakeUpScreen() {
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
Log.e("screen on......", "" + isScreenOn);
if (isScreenOn == false) {
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyLock");
wl.acquire(10000);
PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyCpuLock");
wl_cpu.acquire(10000);
}
}
/*Add notification method use for add icon and title*/
private void addNotification(Map<String, String> data) {
int icon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ?R.drawable.logo_app: R.drawable.logo_app;
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(icon)
//.setSmallIcon(R.drawable.logout_icon)
.setContentTitle(data.get("title") + "")
.setChannelId("channel-01")
.setAutoCancel(true)
.setSound(uri)
.setContentText(data.get("body") + "");
Notification notification = new Notification();
Log.e(TAG, "titlee" + data.get("title"));
// Cancel the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
if (sound)
notification.defaults |= Notification.DEFAULT_SOUND;
if (vibration)
notification.defaults |= Notification.DEFAULT_VIBRATE;
builder.setDefaults(notification.defaults);
try {
if (Sharedpref.getUserPreferences(this, Global.USER_Profile) == null) {
/*start login activity*/
Intent i = new Intent(this, LoginActivity.class);
startActivity(i);
} else {
/*notification send with type calling */
if (data.get("not_type").equals("calling")) {
if (data.get("type").equals("name")) {
Log.e(TAG, "ttt--" + type);
Intent notificationIntent = new Intent(this, CallingActivity.class);
notificationIntent.putExtra("notification_room_id", body);
notificationIntent.putExtra("data", dataa);
notificationIntent.putExtra("calling", "calling");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
builder.setSound(uri);
builder.setAutoCancel(true);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Hello";// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel("channel-01", name, importance);
manager.createNotificationChannel(mChannel);
}
manager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), builder.build());
}
} else {
/* notification get no type then go to the else case*/
Intent notificationIntent1 = new Intent(this, HomeActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
builder.setSound(uri);
builder.setAutoCancel(true);
}
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
此通知类代码显示所有情况下的通知,例如,例如,手机处于运行状态,手机处于后台状态以及手机处于锁定状态
此代码适用于所有设备
我希望它能对您有所帮助。