【问题标题】:Notification sound in lock-screen锁屏提示音
【发布时间】:2017-09-19 16:35:35
【问题描述】:

我正在开发我的第一个 Android 应用程序,但问题来了。 我已经用 Firebase 设置了通知声音。我的应用目前可以工作,只有在屏幕上打开应用时才会播放声音...如果屏幕被锁定,我会在屏幕上收到通知,但没有播放声音...我该如何解决?

下面是部分代码: https://imghost.io/images/2017/09/19/Cattura.png

【问题讨论】:

    标签: android push-notification


    【解决方案1】:

    试试这个。

    try {
        // Get default notification uri object.
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    然后将此 uri 设置为通知构建器对象中的声音,例如

    notificationBuilder.setSound(notification)
    

    查看this原答案

    【讨论】:

    • 更新了答案。另外,删除其他与添加声音相关的代码。
    【解决方案2】:
    private void showNotification(String msg) 
    {
            private Context mContext = getApplicationContext();
            private NotificationManager  mNotificationManager = (NotificationManager)
                    this.getSystemService(Context.NOTIFICATION_SERVICE);
    
    
    
            ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> services = activityManager
                    .getRunningTasks(Integer.MAX_VALUE);
            boolean isActivityFound = false;
    
            if (services.get(0).topActivity.getPackageName().toString()
                    .equalsIgnoreCase(getPackageName().toString())) {
                isActivityFound = true;
            }
            Intent openIntent = null;
            if (isActivityFound) {
                openIntent = new Intent();
            } else {
                openIntent = new Intent(this, MainActivity.class);
                openIntent.putExtra("message", msg);
            }
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                    openIntent, PendingIntent.FLAG_ONE_SHOT);
    
    
            if (msg != null && (!msg.isEmpty())) {
                NotificationCompat.Builder mBuilder =
                        new NotificationCompat.Builder(this)
                                .setDefaults(Notification.DEFAULT_ALL)
                                .setVibrate(new long[]{100, 250, 100, 250, 100, 250})
                                .setAutoCancel(true)
                                .setColor(getResources().getColor(R.color.activity_toolbar_color))
                                .setContentTitle(theTitle)
                                .setStyle(new NotificationCompat.BigTextStyle()
                                        .bigText(Html.fromHtml(msg)))
                                .setPriority(Notification.PRIORITY_MAX)
                                .setContentText(Html.fromHtml(msg));
    
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    mBuilder.setSmallIcon(R.drawable.notification_icon1);
                } else {
                    mBuilder.setSmallIcon(R.drawable.notification_icon);
                }
    
                mBuilder.setContentIntent(contentIntent);
    
    
                int notificationNumber = 0;
    
                mNotificationManager.notify(notificationNumber, mBuilder.build());
    
            }
        }
    

    【讨论】:

    • 只有在屏幕上打开应用时才会发出提示音!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多