c# – 如何使用从远程通知创建的本地通知来激活活动

前端之家收集整理的这篇文章主要介绍了c# – 如何使用从远程通知创建的本地通知来激活活动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经成功创建了一个启动活动的本地通知,但是由于某种原因,当本地通知是从远程通知的处理程序内创建时,当用户轻击本地通知时,该活动不会启动.没有错误或异常似乎被抛出.

以下是创建本地通知代码.注意我使用Xamarin.
我不知道这是否可能是某种权限相关的(远程通知处理程序可能无法创建意图开始活动?).

  1. private void CreateNotification(string title,string desc) {
  2. var uiIntent = new Intent(this,typeof(ConversationActivity));
  3.  
  4. var stackBuilder = TaskStackBuilder.Create(this);
  5. stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(ConversationActivity)));
  6. stackBuilder.AddNextIntent(uiIntent);
  7.  
  8. PendingIntent pendingIntent = stackBuilder.GetPendingIntent(0,(int)PendingIntentFlags.UpdateCurrent);
  9.  
  10. var notification = new NotificationCompat.Builder(this)
  11. .SetAutoCancel(true) // Remove the notification once the user touches it
  12. .SetContentIntent(pendingIntent)
  13. .SetContentTitle(title)
  14. .SetSmallIcon(Resource.Drawable.AppIcon)
  15. .SetContentText(desc)
  16. .SetDefaults((int)(NotificationDefaults.Sound | NotificationDefaults.Vibrate))
  17. ;
  18.  
  19. // Set the notification info
  20. // we use the pending intent,passing our ui intent over which will get called
  21. // when the notification is tapped.
  22. var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
  23. notificationManager.Notify(1,notification.Build());
  24. }

解决方法

我仍然不确定我的原始尝试有什么问题,但是我确实发现我可以通过改变Intent来使用组件名而不是动作或活动类型来修复它:
  1. private void SendNotification() {
  2. var nMgr = (NotificationManager)this.GetSystemService(NotificationService);
  3. var notification = new Notification(Resource.Drawable.AppIcon,"Incoming Dart");
  4. var intent = new Intent();
  5. intent.SetComponent(new ComponentName(this,"dart.androidapp.ContactsActivity"));
  6. var pendingIntent = PendingIntent.GetActivity(this,intent,0);
  7. notification.SetLatestEventInfo(this,"You've got something to read","You have received a message",pendingIntent);
  8. nMgr.Notify(0,notification);
  9. }

猜你在找的C#相关文章