开发者社区> 问答> 正文

xamarin中如何实现通知接收?基于NetStandard2.0

我找了很久发现有java下有一下sdk,在nuget中没找到相应包 import com.alibaba.sdk.android.push.MessageReceiver; import com.alibaba.sdk.android.push.notification.CPushMessage; import com.alibaba.sdk.android.push.notification.PushData; import com.alibaba.sdk.android.push.notification.b; import com.alibaba.sdk.android.push.notification.d; import com.alibaba.sdk.android.push.notification.e; import com.aliyun.ams.emas.push.AgooMessageReceiver; import com.aliyun.ams.emas.push.notification.CPushMessage; 请问大神,下面的过程怎么在C#中实现: public abstract class MessageReceiver extends AgooMessageReceiver { private d mMessageNotification = new d();

public void onReceive(Context paramContext, Intent paramIntent) { super.onReceive(paramContext, paramIntent); }

public boolean showNotificationNow(Context paramContext, Map<String, String> paramMap) { return (d.a(paramMap) || !e.a(paramContext)); }

public Notification customNotificationUI(Context paramContext, Map<String, String> paramMap) { Notification notification = customNotificationUI(paramContext, PushData.parse(paramContext, paramMap)); if (notification != null) return notification; b b = this.mMessageNotification.a(paramContext, paramMap); return this.mMessageNotification.a(paramContext, b); }

public Notification customNotificationUI(Context paramContext, PushData paramPushData) { return null; }

public void onNotificationReceivedWithoutShow(Context paramContext, String paramString1, String paramString2, Map<String, String> paramMap, int paramInt, String paramString3, String paramString4) { onNotificationReceivedInApp(paramContext, paramString1, paramString2, paramMap, paramInt, paramString3, paramString4); }

public void onNotificationShow(Context paramContext, String paramString1, String paramString2, Map<String, String> paramMap) { onNotification(paramContext, paramString1, paramString2, paramMap); }

public void onMessageArrived(Context paramContext, CPushMessage paramCPushMessage) { onMessage(paramContext, CPushMessage.from(paramCPushMessage)); }

public void onNotificationOpened(Context paramContext, String paramString1, String paramString2, String paramString3, int paramInt) { if (paramInt == 4) { onNotificationClickedWithNoAction(paramContext, paramString1, paramString2, paramString3); } else { onNotificationOpened(paramContext, paramString1, paramString2, paramString3); } }

public void onNotificationRemoved(Context paramContext, String paramString1, String paramString2, String paramString3, int paramInt, String paramString4) { onNotificationRemoved(paramContext, paramString4); }

protected abstract void onNotification(Context paramContext, String paramString1, String paramString2, Map<String, String> paramMap);

protected abstract void onMessage(Context paramContext, CPushMessage paramCPushMessage);

protected abstract void onNotificationOpened(Context paramContext, String paramString1, String paramString2, String paramString3);

protected abstract void onNotificationClickedWithNoAction(Context paramContext, String paramString1, String paramString2, String paramString3);

protected abstract void onNotificationRemoved(Context paramContext, String paramString);

protected abstract void onNotificationReceivedInApp(Context paramContext, String paramString1, String paramString2, Map<String, String> paramMap, int paramInt, String paramString3, String paramString4); }

展开
收起
1672840347443396 2020-12-29 10:37:17 933 0
1 条回答
写回答
取消 提交回答
  • 下一站是幸福
    using Android.App;
    using Android.Content;
    using Android.Graphics;
    using Android.Media;
    using Android.OS;
    using Android.Support.V4.App;
    using Consumer.Mobile.Infra;
    using Consumer.Mobile.Services.PushNotification;
    using Java.Lang;
    using XLabs.Ioc;
    using TaskStackBuilder = Android.Support.V4.App.TaskStackBuilder;
    
    namespace Consumer.Mobile.Droid.PushNotification
    {
        [Service]
        public class MyIntentService : IntentService
        {
            private readonly ILogger _logger;
            private readonly IPushNotificationService _notificationService;
            private readonly IPushNotificationRegister _pushNotificationRegister;
    
            public MyIntentService()
            {
                _logger = Resolver.Resolve<ILogger>();
                _notificationService = Resolver.Resolve<IPushNotificationService>();
                _pushNotificationRegister = Resolver.Resolve<IPushNotificationRegister>();
            }
    
            static PowerManager.WakeLock _sWakeLock;
            static readonly object Lock = new object();
    
    
            public static void RunIntentInService(Context context, Intent intent)
            {
                lock (Lock)
                {
                    if (_sWakeLock == null)
                    {
                        // This is called from BroadcastReceiver, there is no init.
                        var pm = PowerManager.FromContext(context);
                        _sWakeLock = pm.NewWakeLock(
                        WakeLockFlags.Partial, "My WakeLock Tag");
                    }
                }
    
                _sWakeLock.Acquire();
                intent.SetClass(context, typeof(MyIntentService));
                context.StartService(intent);
            }
    
            protected override void OnHandleIntent(Intent intent)
            {
                try
                {
                    Context context = this.ApplicationContext;
                    string action = intent.Action;
    
                    if (action.Equals("com.google.android.c2dm.intent.REGISTRATION"))
                    {
                        HandleRegistration(context, intent);
                    }
                    else if (action.Equals("com.google.android.c2dm.intent.RECEIVE"))
                    {
                        HandleMessage(context, intent);
                    }
                }
                finally
                {
                    lock (Lock)
                    {
                        //Sanity check for null as this is a public method
                        if (_sWakeLock != null)
                            _sWakeLock.Release();
                    }
                }
            }
    
            private void HandleMessage(Context context, Intent intent)
            {
    
                Intent resultIntent = new Intent(this, typeof(MainActivity));
    
    
                TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this);
    
                var c = Class.FromType(typeof(MainActivity));
                stackBuilder.AddParentStack(c);
                stackBuilder.AddNextIntent(resultIntent);
    
                string alert = intent.GetStringExtra("Alert");
                int number = intent.GetIntExtra("Badge", 0);
    
                var imageUrl = intent.GetStringExtra("ImageUrl");
                var title = intent.GetStringExtra("Title");
    
                Bitmap bitmap = GetBitmap(imageUrl);
    
                PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
    
                NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                    .SetAutoCancel(true) // dismiss the notification from the notification area when the user clicks on it
                    .SetContentIntent(resultPendingIntent) // start up this activity when the user clicks the intent.
                    .SetContentTitle(title) // Set the title
                    .SetNumber(number) // Display the count in the Content Info
                    .SetSmallIcon(Resource.Drawable.Icon) // This is the icon to display
                    .SetLargeIcon(bitmap)
                    .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
                    .SetContentText(alert); // the message to display.
    
                // Build the notification:
                Notification notification = builder.Build();
    
                // Get the notification manager:
                NotificationManager notificationManager =
                    GetSystemService(Context.NotificationService) as NotificationManager;
    
                // Publish the notification:
                const int notificationId = 0;
                notificationManager.Notify(notificationId, notification);
            }
    
            private void HandleRegistration(Context context, Intent intent)
            {
                var token = intent.GetStringExtra("registration_id");
                _logger.Info(this.Class.SimpleName, "Received Token : " + token);
    
                if (_pushNotificationRegister.ShouldSendToken(token))
                {
                    var uid = Android.Provider.Settings.Secure.GetString(MainActivity.Context.ContentResolver, Android.Provider.Settings.Secure.AndroidId);
                    _notificationService.AddPushToken(token, DeviceUtils.GetDeviceType(), uid);
                }
            }
    
    
            private Bitmap GetBitmap(string url)
            {
    
                try
                {
                    System.Net.WebRequest request =
                        System.Net.WebRequest.Create(url);
                    System.Net.WebResponse response = request.GetResponse();
                    System.IO.Stream responseStream =
                        response.GetResponseStream();
                    return BitmapFactory.DecodeStream(responseStream);
    
    
                }
                catch (System.Net.WebException)
                {
                    return null;
                }
    
            }
    
        }
    }
    
    2021-04-02 22:03:08
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
内容驱动游戏分发 立即下载
从“连接”到“交互” 立即下载
低代码开发师(初级)实战教程 立即下载

相关实验场景

更多