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



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

下面是创建本地通知的代码。注意 我正在使用 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, 0, intent, 0);
        notification.SetLatestEventInfo(this, "You've got something to read", "You have received a message", pendingIntent);
        nMgr.Notify(0, notification);
    }

你也应该能够用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);
}

你好安德鲁对我来说,波纹管工作正常

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);

在谈论别的吗?

最新更新