如何在Firebase控制台上为HTTP v1推送消息设置默认声音

时间:2018-08-10 12:38:40

标签: android firebase push-notification firebase-cloud-messaging

此问题特定于Android上版本http v1 的Firebase控制台通知。 iOS可用。

请不要将其设置为重复版本,除非对此特定版本有疑问!

我找不到将默认声音设置为发送适当的推送通知的方法。我已经广泛地查看了the docs,但是它们缺少任何功能示例!

您是否知道通过 Firebase控制台,版本为http v1 ,将键“ sound”设置为默认值,来为Android推送通知使用有效json格式的功能示例?

此格式有效,通知会在所有设备上收到,有些声音会收到,有些则不会。这不是设备问题。它们在设备设置中启用。

const requestBody1 = {
        message: {
            token: deviceToken,
            sound: "default",
            notification: {
                body: theMsg,
                title: theTitle
            },
            apns: {
                "payload": {
                    "aps": {
                        "sound": "default"
                    }
                }
            }
        }

};

我已经测试了许多不同的json(不用担心引号)格式来获取声音:“默认”。它们都返回一些错误:

IE

const requestBody = {
    "message":{
        "android": {
            "collapse_key": "a collapse key",
            "priority": "normal",
            "ttl": "10s",
            "restricted_package_name": "com.test.app",
            "data": {
                "dummydata" : "dummydata",
            },
            "notification": {
                "title": "one title",
                "body": "one message",
                "sound": "default"
            }
        }
    }

};

谢谢。

1 个答案:

答案 0 :(得分:0)

我得到了如下的JSON数据:

public void onMessageReceived(RemoteMessage message) {
        Session session=new Session(this);
        try {
            JSONObject data=new JSONObject(message.getData().get("data"));
            System.out.println("------------------- Notification Received : " + data.toString());
            if(!session.getUserId().equals(null)) {
                if(!session.getUserId().equals("")) {
                    System.out.println("---------------------- USERID : " + session.getUserId());
                    sendMyNotification(data.get("body").toString(), data.get("title").toString());
                }
            }
        } catch (JSONException e) {
            System.out.println("----------------Error in onMessageReceived : " + e.getMessage());
            e.printStackTrace();
        }catch (Exception e){
            System.out.println("----------------Error in onMessageReceived : " + e.getMessage());
        }

    }

我发送的Json数据如下:

{
 "to" : "User Token" : {

    "data" :{
         "body" : "Hello, How are you?",
         "title": "Notification From API"
    }
 }
}

您可以像这样发送默认声音以进行通知,但是如果您有声音uri,则可以通过设置setSound()的属性直接进行设置:

,您可以像这样将字符串转换为uri:Uri myUri = Uri.parse("Your String data")

Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            @SuppressLint("WrongConstant")
            NotificationChannel notificationChannel=new NotificationChannel("my_notification","n_channel",NotificationManager.IMPORTANCE_MAX);
            notificationChannel.setDescription("description");
            notificationChannel.setName("Channel Name");
            notificationManager.createNotificationChannel(notificationChannel);
        }
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.listlogo)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.tlogo))
                    .setContentTitle(title)
                    .setContentText(message)
                    .setAutoCancel(true)
                    .setSound(soundUri)
                    .setContentIntent(pendingIntent)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setPriority(NotificationManager.IMPORTANCE_MAX)
                    .setOnlyAlertOnce(true)
                    .setChannelId("my_notification")
                    .setColor(Color.parseColor("#3F5996"));
            notificationManager.notify(0, notificationBuilder.build());
    }