通知不显示Android设备上。 (天青通知集线器)

问题描述 投票:0回答:1

我试图做一个推送通知发送到我的Android模拟器。当发送的通知时,它接收该通知,但不显示它。

我使用这个代码,以显示它。

void SendNotification(string messageBody)
    {
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                                                                .SetContentTitle("FCM Message")
                                                                .SetSmallIcon(Resource.Drawable.icon)
                                                                .SetContentText(messageBody)
                                                                .SetChannelId("my-chanel")
                                                                .SetAutoCancel(true)
                                                                .SetContentIntent(pendingIntent)
                                                                .SetSound(); 

        var notificationManager = NotificationManager.FromContext(this);

        notificationManager.Notify(0, notificationBuilder.Build());
    }

但是,当收到通知空话正好与我的设备的日志给我这样的:

[Mono] Assembly Ref addref ODEON.Android[0xa31db5c0] -> System.Core[0xa1f2f900]: 7
[MyFirebaseMsgService] From: 241571420247
[MyFirebaseMsgService] noti: 
[Mono] DllImport searching in: '__Internal' ('(null)').
[Mono] Searching for 'java_interop_jnienv_call_boolean_method'.
[Mono] Probing 'java_interop_jnienv_call_boolean_method'.
[Mono] Found as 'java_interop_jnienv_call_boolean_method'.
[Mono] Assembly Ref addref Xamarin.Android.Support.Compat[0xa31dca60] -> Java.Interop[0xa1f2f780]: 8
[Notification] Use of stream types is deprecated for operations other than volume control
[Notification] See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case 

谁能告诉我为什么不显示。提前致谢!

azure push-notification xamarin.android android-notifications
1个回答
1
投票

如果发送通知方法被称为使用下面的代码显示通知:

        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)
        .SetContentTitle("Title")
        .SetContentText(messageBody)
        .SetAutoCancel(true)
        .SetContentIntent(pendingIntent);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);
    notificationManager.Notify(0, notificationBuilder.Build());

此外,如果有,我相信Android的V 8+设备,你会看到它您注册为您的MainActivity onCreate方法通知通道

void CreateNotificationChannel()
    {
        if (Build.VERSION.SdkInt < BuildVersionCodes.O)
        {
            // Notification channels are new in API 26 (and not a part of the
            // support library). There is no need to create a notification 
            // channel on older versions of Android.
            return;
        }

        var channel = new NotificationChannel(CHANNEL_ID, "FCM Notifications", NotificationImportance.Default)
                      {
                          Description = "Firebase Cloud Messages appear in this channel"
                      };

        var notificationManager = (NotificationManager) GetSystemService(NotificationService);
        notificationManager.CreateNotificationChannel(channel);
    }
© www.soinside.com 2019 - 2024. All rights reserved.