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

And*_*ott 10 c# notifications android push-notification xamarin

我已成功创建了一个启动活动的本地通知,但由于某种原因,当从远程通知处理程序中创建此本地通知时,当用户点击本地通知时,活动不会启动.似乎没有抛出任何错误或异常.

以下是创建本地通知的代码.注意我正在使用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());
}
Run Code Online (Sandbox Code Playgroud)

And*_*ott 11

我仍然不确定我的原始尝试有什么问题,但我确实发现我可以通过更改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, 0, intent, 0);
        notification.SetLatestEventInfo(this, "You've got something to read", "You have received a message", pendingIntent);
        nMgr.Notify(0, notification);
    }
Run Code Online (Sandbox Code Playgroud)