我已经成功创建了一个启动活动的本地通知,但是由于某种原因,当本地通知是从远程通知的处理程序内创建时,当用户轻击本地通知时,该活动不会启动.没有错误或异常似乎被抛出.
以下是创建本地通知的代码.注意我使用Xamarin.
我不知道这是否可能是某种权限相关的(远程通知处理程序可能无法创建意图开始活动?).
private void CreateNotification(string title,string desc) { var uiIntent = new Intent(this,typeof(ConversationActivity)); var stackBuilder = TaskStackBuilder.Create(this); stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(ConversationActivity))); stackBuilder.AddNextIntent(uiIntent); PendingIntent pendingIntent = stackBuilder.GetPendingIntent(0,(int)PendingIntentFlags.UpdateCurrent); var notification = new NotificationCompat.Builder(this) .SetAutoCancel(true) // Remove the notification once the user touches it .SetContentIntent(pendingIntent) .SetContentTitle(title) .SetSmallIcon(Resource.Drawable.AppIcon) .SetContentText(desc) .SetDefaults((int)(NotificationDefaults.Sound | NotificationDefaults.Vibrate)) ; // Set the notification info // we use the pending intent,passing our ui intent over which will get called // when the notification is tapped. var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager; notificationManager.Notify(1,notification.Build()); }
解决方法
我仍然不确定我的原始尝试有什么问题,但是我确实发现我可以通过改变Intent来使用组件名而不是动作或活动类型来修复它:
private void SendNotification() { var nMgr = (NotificationManager)this.GetSystemService(NotificationService); var notification = new Notification(Resource.Drawable.AppIcon,"Incoming Dart"); var intent = new Intent(); intent.SetComponent(new ComponentName(this,"dart.androidapp.ContactsActivity")); var pendingIntent = PendingIntent.GetActivity(this,intent,0); notification.SetLatestEventInfo(this,"You've got something to read","You have received a message",pendingIntent); nMgr.Notify(0,notification); }