首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android推送通知声音仅在app前台播放,app在后台不播放声音

Android推送通知声音仅在app前台播放,app在后台不播放声音
EN

Stack Overflow用户
提问于 2017-01-10 14:59:09
回答 4查看 16K关注 0票数 13

我正在使用FCM的推送通知下面的代码播放声音时,收到通知

代码语言:javascript
复制
 public void playNotificationSound() {
        try {

            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone r = RingtoneManager.getRingtone(mContext, notification);
            r.play();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

我正在调用此OnMessageReceived方法,但声音仅在应用程序处于前台时播放,而不是在应用程序在后台时播放

代码语言:javascript
复制
 @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.e(TAG, "From: " + remoteMessage.getFrom());

        if (remoteMessage == null)
            return;

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
            handleNotification(remoteMessage.getNotification().getBody());
        }

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

            try {
                JSONObject json = new JSONObject(remoteMessage.getData().toString());
                handleDataMessage(json);
            } catch (Exception e) {
                Log.e(TAG, "Exception: " + e.getMessage());
            }
        }
    }





 private void handleNotification(String message) {
        if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
            // app is in foreground, broadcast the push message
            Intent pushNotification = new Intent(config.PUSH_NOTIFICATION);
            pushNotification.putExtra("message", message);
            LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

            // play notification sound
            NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
            notificationUtils.playNotificationSound();
        }else if (NotificationUtils.isAppIsInBackground(getApplicationContext())){
            // If the app is in background, firebase itself handles the notification
            NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
            notificationUtils.playNotificationSound();
        }
    }
EN

回答 4

Stack Overflow用户

发布于 2017-06-02 02:55:06

由于在发送通知对象时不会调用onMessageReceived(),因此我创建了一个BroadcastReceiver来在通知到达时处理它:

代码语言:javascript
复制
public class NotificationReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    playNotificationSound(context);
}

public void playNotificationSound(Context context) {
    try {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(context, notification);
        r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

并将其添加到清单中。接收方负责播放通知铃声。

代码语言:javascript
复制
 <receiver
        android:name=".notification.NotificationReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
 </receiver>
票数 13
EN

Stack Overflow用户

发布于 2017-11-10 11:35:22

您需要在Firebase Notification Composer的高级设置下启用声音。:)

票数 1
EN

Stack Overflow用户

发布于 2018-07-01 00:01:58

代码语言:javascript
复制
private void sendNotification(String messageBody, Intent intent) {

        //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent/*.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)*/,
                PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentIntent(pendingIntent);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);//<--

        Bitmap bitmap = getBitmapfromUrl(postImageUrl);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.notification_recive)


                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.notification_recive))
                .setContentTitle(postTitle + "")
                .setStyle(new NotificationCompat.BigPictureStyle()
                        .setSummaryText(postTitle + "")
                        .bigPicture(bitmap))
                .setContentText(messageBody)
                .setLights(getResources().getColor(R.color.blue),1000,1500)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)//<--
                .setContentIntent(pendingIntent);


        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41562915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档