通知未显示在android设备上。 (Azure通知中心)

时间:2019-02-01 13:02:12

标签: azure push-notification xamarin.android android-notifications

我正在尝试向我的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 

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

1 个答案:

答案 0 :(得分: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);
    }