【问题标题】:Android Notifications while screen is off屏幕关闭时的 Android 通知
【发布时间】:2020-05-14 08:56:40
【问题描述】:

我正在尝试在屏幕关闭时调用通知。 这适用于华为 p10 Lite(序列号:HUAWEIWAS-LX1AAndroid 8.0.0),但不适用于 Android 10 三星 Galaxy Tab S6(QP1A.190711.020)。不过,通知可以通过锁屏。

这是我的代码:

    fun Notifications () {
        mNotification = NotificationCompat.Builder(this, CHANNEL_ID)
            .setDefaults(NotificationCompat.DEFAULT_VIBRATE)
            .setVibrate(longArrayOf(1000))
            .setSmallIcon(R.drawable.icon)
            .setSound(soundUri)
            .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.icon))
            .setCategory(NotificationCompat.CATEGORY_ALARM)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentTitle("Title")
            .setContentText("Text")
            .setChannelId(CHANNEL_ID)
            .setAutoCancel(true)
            .setOngoing(true)
            .setColor(NotificationCompat.COLOR_DEFAULT)
            .setColorized(true)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(CHANNEL_ID, "my-app", NotificationManager.IMPORTANCE_HIGH).apply {
                description = "Test"
            }
            channel.importance = NotificationManager.IMPORTANCE_HIGH
            channel.shouldVibrate()
            channel.enableVibration(true)
            channel.vibrationPattern=longArrayOf(1000, 1000, 1000, 1000)
            channel.canBypassDnd()
            channel.lightColor = NotificationCompat.COLOR_DEFAULT
            channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
            mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            mNotificationManager.createNotificationChannel(channel)
        }
    }

MainActivity 类的成员:

    val soundUri: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
    lateinit var mNotification : NotificationCompat.Builder
    lateinit var mNotificationManager : NotificationManager
    val mNotificationID: Int = 1
    val CHANNEL_ID : String  = "com.example.myapp"

这是我的使用方法:

with(NotificationManagerCompat.from(this)) {
    notify(mNotificationID, mNotification.build())
}

【问题讨论】:

    标签: android android-notifications


    【解决方案1】:

    我有同样的问题。所以我在 onMessageReceived() 上制作唤醒设备代码

    @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
    
            Dlog.d("From: " + remoteMessage.getFrom());
    
            if (remoteMessage.getData().size() > 0) {
                Dlog.d("Message data payload: " + remoteMessage.getData().get("body"));
            }
    
            if (remoteMessage.getNotification() != null) {
                Dlog.d("Message Notification Body: " + remoteMessage.getNotification().getBody());
            }
    
            //wakeup devices when sleep.
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE );
            @SuppressLint("InvalidWakeLockTag")
            PowerManager.WakeLock wakeLock = pm.newWakeLock( PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG" );
            wakeLock.acquire(3000);
    
            sendNotification(remoteMessage.getData().get("body"));
        }
    

    并根据需要在 sendNotification() 中设置通知。并且有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-18
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多