在安卓系统中,如何在锁屏界面上显示通知?

6

我已经前往以下位置:

  • 设置
  • 通知和状态栏
  • 应用通知
  • [应用名称]
  • 启用锁屏通知

然而,在我的安卓手机(小米红米5A,Nougat)的锁屏界面上没有看到通知。顺便说一下,我是本地通知,没有推送通知或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())
    }
}

这是一个小米的问题。长按您的通知,然后打开“锁屏通知”。 - Aaditya Brahmbhatt
2个回答

4

试试这个...

您可以使用通知渠道的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())
}

通知频道仅适用于API级别26及以上。我已将最低API级别设置为19。 - mownier

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();
    }
}
 }
  • 这个通知类代码可以在所有情况下显示通知,例如- 当您的手机处于运行状态、后台状态和锁定状态时

  • 此代码适用于所有设备

  • 它完全取决于JSON响应


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接