手机锁定时不会创建通知声音

时间:2017-04-17 06:25:30

标签: android firebase firebase-cloud-messaging

我正在开发一个聊天应用程序,我正在使用Firebase云消息传递。通知正常。手机锁定时,会正确接收通知,但不会生成声音。

感谢任何帮助。

FCMMessagingservice.java

package com.synergywebdesigners.nima;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.view.WindowManager;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import static android.app.Notification.VISIBILITY_PUBLIC;
import static com.android.volley.VolleyLog.TAG;


/**
 * Created by Ashish Shahi on 13-04-2017.
 */

public class FcmMessagingService extends FirebaseMessagingService {

    long[] pattern = {500,500,500,500,500,500,500,500,500};
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        PowerManager pm = (PowerManager)getSystemService(
                Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(
                PowerManager.SCREEN_DIM_WAKE_LOCK
                        | PowerManager.ON_AFTER_RELEASE,
                TAG);


        String title = remoteMessage.getNotification().getTitle();
        String message = remoteMessage.getNotification().getBody();
        Intent intent = new Intent(this, Memberlist.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)
                .setSmallIcon(R.mipmap.ic_action_title)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent)
                .setDefaults(0)
                .setLights(Color.RED, 500, 500)
                .setPriority(Notification.PRIORITY_HIGH)
                .setOngoing(true)
                ;
        wl.acquire();
        // ... do work...
        wl.release();
        /*notificationBuilder.setLights(Color.BLUE, 500, 500);
        long[] pattern = {500,500,500,500,500,500,500,500,500};
        notificationBuilder.setVibrate(pattern);*/
        notificationBuilder.setStyle(new NotificationCompat.InboxStyle());
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());

    }
}

1 个答案:

答案 0 :(得分:0)

转到FCM CONSOLE并启用声音并定义声音' ==>'默认&#39 ;; 并转到php cansole并键入以下代码'sound'==>'default';,它们正常工作