加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 03:45:17 所属栏目:百科 来源:网络整理
导读:我已经成功创建了一个启动活动的本地通知,但是由于某种原因,当本地通知是从远程通知的处理程序内创建时,当用户轻击本地通知时,该活动不会启动.没有错误或异常似乎被抛出. 以下是创建本地通知的代码.注意我使用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());
}

解决方法

我仍然不确定我的原始尝试有什么问题,但是我确实发现我可以通过改变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);
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读