首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >当应用程序在后台时,自定义通知音不起作用

当应用程序在后台时,自定义通知音不起作用
EN

Stack Overflow用户
提问于 2019-04-28 14:41:58
回答 1查看 1.5K关注 0票数 1

我正在尝试使用自定义通知音调。当应用程序运行时,音调工作得很好,但当应用程序在后台时,会播放默认音调。此外,当应用程序不活动时接收到通知消息时,振动也不工作

我尝试了以下几种方法:

我的Messenging Service类。

代码语言:javascript
复制
public class MyFirebaseMessagingService extends FirebaseMessagingService{

private static final String TAG = "MyFirebaseMsgService";
Utilities utils = new Utilities();
Notification notification;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage.getData() != null) {
        sendNotification(remoteMessage.getData().get("message"));
        Log.e(TAG,remoteMessage.getData().get("message"));
    }else{
        utils.print(TAG,"FCM Notification failed");
    }
}
private void sendNotification(String messageBody) {
    long[] v = {500,1000};

    if (!isAppIsInBackground(getApplicationContext())) {

        utils.print(TAG,"foreground");
        Log.e(TAG,"Notifcation"+messageBody);
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("push", true);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_ONE_SHOT);


            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(messageBody)
                    .setAutoCancel(false)
                    .setVibrate(v)
                    .setContentIntent(pendingIntent);

            notificationBuilder.setSmallIcon(getNotificationIcon(notificationBuilder), 1);

        notification = notificationBuilder.build();
        notification.sound=Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.alert_tone);
        notification.defaults=Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;

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

            notificationManager.notify(0, notification);
    }
    else{
        if(messageBody.equalsIgnoreCase("New Incoming Ride")){
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                   PendingIntent.FLAG_ONE_SHOT);

            Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(messageBody)
                    .setAutoCancel(false)
                    .setVibrate(v)
                    .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.alert_tone))
                   .setContentIntent(pendingIntent);

            notificationBuilder.setSmallIcon(getNotificationIcon(notificationBuilder), 1);
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notification = notificationBuilder.build();
            notification.defaults=Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;
            notification.sound=Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.alert_tone);
            notificationManager.notify(0, notification);
        }
        else{
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("push", true);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_ONE_SHOT);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(messageBody)
                    .setAutoCancel(false)
                    .setVibrate(v)
                    .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.alert_tone))
                   .setContentIntent(pendingIntent);

            notificationBuilder.setSmallIcon(getNotificationIcon(notificationBuilder), 1);
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notification = notificationBuilder.build();
            notification.sound=Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.alert_tone);
            notificationManager.notify(0, notification);
        }
    }
}

private int getNotificationIcon(NotificationCompat.Builder notificationBuilder) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
        notificationBuilder.setColor(ContextCompat.getColor(getApplicationContext(),R.color.colorPrimary));
        return  R.mipmap.ic_launcher;
    }else {
        return R.mipmap.ic_launcher;
    }
}
public static boolean isAppIsInBackground(Context context) {
    boolean isInBackground = true;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
        List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
            if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                for (String activeProcess : processInfo.pkgList) {
                    if (activeProcess.equals(context.getPackageName())) {
                        isInBackground = false;
                    }
                }
            }
        }
    } else {
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        ComponentName componentInfo = taskInfo.get(0).topActivity;
        if (componentInfo.getPackageName().equals(context.getPackageName())) {
            isInBackground = false;
        }
    }
    return isInBackground;
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55887711

复制
相关文章

相似问题

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