【问题标题】:How to activate an activity using a local notification created from a remote notification如何使用从远程通知创建的本地通知激活活动
【发布时间】:2014-01-14 13:37:43
【问题描述】:

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

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

【问题讨论】:

    标签: c# android notifications push-notification xamarin


    【解决方案1】:

    我仍然不确定我最初的尝试出了什么问题,但我确实发现我可以通过将 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);
        }
    

    【讨论】:

    • 谢谢,@CRSardar。但是您知道为什么活动类型或操作名称不起作用吗?
    【解决方案2】:

    您也应该可以使用 typeof(...) 来调用它。您首先发布的代码与后者完全不同。试试这是否适合你:

    private void SendNotification() 
    {
        var nMgr = (NotificationManager)this.GetSystemService(NotificationService);
        var notification = new Notification(Resource.Drawable.AppIcon, "Incoming Dart");
        var intent = new Intent(this, typeof(ContactsActivity));
        //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);
    }
    

    【讨论】:

    • 这看起来很像我的第一次迭代(未发布),并且工作正常——除非在远程通知处理程序的上下文中执行。
    【解决方案3】:

    你好 Andrew 对我来说工作正常

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = new Notification(R.drawable.ic_launcher, "Hello Chitta!", System.currentTimeMillis());
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("message/rfc822");
            intent.putExtra(Intent.EXTRA_EMAIL  , new String[]{"jdsjdjbdf@gmail.com"});
            intent.putExtra(Intent.EXTRA_SUBJECT, "Hello CR!");
            intent.putExtra(Intent.EXTRA_TEXT   , "This is the body of email");
    
            PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
            notification.setLatestEventInfo(getApplicationContext(), "Send an e-mail", "Ha ha", pendingIntent);
            notificationManager.notify(742134, notification);
    

    在谈论其他事情吗?

    【讨论】:

    • 问题不在于发送电子邮件。这是关于弹出我自己的一项活动。
    • 您能否详细解释一下您的用例(逐个状态)?
    • 我不知道该怎么做。我有一个远程通知,它应该创建一个本地通知,当用户点击它时,它应该启动我的一项活动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 2010-11-25
    • 2020-12-31
    • 1970-01-01
    相关资源
    最近更新 更多