[Xamarin]无法从服务启动通知(java.lang.reflect.InvocationTargetException)

时间:2015-09-08 14:46:17

标签: c# service xamarin notifications

我正在尝试在服务中发送通知,但我收到了java.lang.reflect.InvocationTargetException。 这是我在服务中调用的函数" OnStartCommand" ... 我从例子中复制粘贴了所有代码,因为我试图实现这一点,但我被困在这里。

// Pass the current button press count value to the next activity:
        Bundle valuesForActivity = new Bundle();
        valuesForActivity.PutInt ("count", 3);

        // When the user clicks the notification, SecondActivity will start up.
        Intent resultIntent = new Intent(this, typeof (MainActivity));

        // Pass some values to SecondActivity:
        resultIntent.PutExtras (valuesForActivity);

        // Construct a back stack for cross-task navigation:
        TaskStackBuilder stackBuilder = TaskStackBuilder.Create (this);
        stackBuilder.AddParentStack (Java.Lang.Class.FromType(typeof(MainActivity
        )));
        stackBuilder.AddNextIntent (resultIntent);

        // Create the PendingIntent with the back stack:            
        PendingIntent resultPendingIntent = 
            stackBuilder.GetPendingIntent (0, (int)PendingIntentFlags.UpdateCurrent);

        // Build the notification:
        NotificationCompat.Builder builder = new NotificationCompat.Builder (this)
            .SetAutoCancel (true)                    // Dismiss from the notif. area when clicked
            .SetContentIntent (resultPendingIntent)  // Start 2nd activity when the intent is clicked.
            .SetContentTitle ("Button Clicked")      // Set its title
            .SetNumber (1)                       // Display the count in the Content Info
            .SetSmallIcon(Resource.Drawable.Icon)  // Display this icon
            .SetContentText (String.Format(
                "The button has been clicked {0} times")); // The message to display.

        // Finally, publish the notification:
        NotificationManager notificationManager = 
            (NotificationManager)GetSystemService(Context.NotificationService);
        notificationManager.Notify(ButtonClickNotificationId, builder.Build());

我不明白为什么我会遇到这个问题,但这是我第一次提供服务和通知,请求帮助我们

0 个答案:

没有答案