【问题标题】:Push Notification Sent Successfully with AppCenter But there is no sound when notification received ( xamarin android )使用 AppCenter 成功发送推送通知但收到通知时没有声音(xamarin android)
【发布时间】:2019-05-04 10:50:20
【问题描述】:

我在 xamarin android 项目中使用 AppCenter 推送通知。通知成功收到,但没有声音。请指导我如何处理通知声音。

protected override void OnStart()
    {
        AppCenter.Start("c71c0b73-a242-469e-ad97-990ca0853f9b", typeof(Push));
        CustomProperties properties = new CustomProperties();
        properties.Set("Sound", true);
        AppCenter.SetCustomProperties(properties);
    }

【问题讨论】:

    标签: xamarin.android visual-studio-app-center android-push-notification


    【解决方案1】:

    为通知添加声音,您可以使用 NotificationChannel.SetSound(),将 mp3 文件添加到您的 Android 项目的 Resources/raw/soundFile.mp3 下,并将构建设置为 Android Resource )

    private void createNotificationChannel()
        {          
                var channelName = GetString(Resource.String.noti_chan_urgent);
                var channelDescription = GetString(Resource.String.noti_chan_urgent_description);
    
                // set the vibration patterm for the channel
                long[] vibrationPattern = { 100, 200, 300, 400, 500, 400, 300, 200, 400 };
    
                // Creating an Audio Attribute
                var alarmAttributes = new AudioAttributes.Builder()
                    .SetContentType(AudioContentType.Sonification)
                    .SetUsage(AudioUsageKind.Notification).Build();
    
                    // Create the uri for the alarm file                 
                Android.Net.Uri alarmUri = Android.Net.Uri.Parse(${ContentResolver.SchemeAndroidResource}://{Context.PackageName}/{Resource.Raw.soundFile}");
    
    
                // create chan1  which is the urgent notifications channel
                var chan1 = new NotificationChannel(PRIMARY_CHANNEL_ID, channelName, NotificationImportance.High)
                {
                    Description = channelDescription
                };
    
    
                // set the channel properties
                chan1.EnableLights(true);
                chan1.LightColor = Color.Red;
                chan1.SetSound(alarmUri, alarmAttributes);
                chan1.EnableVibration(true);
                chan1.SetVibrationPattern(vibrationPattern);               
                chan1.SetBypassDnd(true);
                chan1.LockscreenVisibility = NotificationVisibility.Public;
    
                // finally create the notification channel
                var manager = (NotificationManager)GetSystemService(NotificationService);
                manager.CreateNotificationChannel(chan1);
        }
    

    【讨论】:

    • 我想随时向用户发送推送通知,我该如何实现呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 2015-01-14
    • 1970-01-01
    • 2016-10-23
    相关资源
    最近更新 更多