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

时间:2013-12-26 23:26:00

标签: c# android notifications 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());
}

3 个答案:

答案 0 :(得分: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);
    }

答案 1 :(得分:0)

您也应该可以使用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);
}

答案 2 :(得分:-1)

你好安德鲁对我说吼正常工作

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

正在谈论其他事情吗?