如何仅在应用程序处于休眠模式时从便携式项目调用pushNotification

时间:2017-10-06 01:08:35

标签: xaml xamarin xamarin.ios xamarin.forms xamarin.android

我使用CrossPlateForm技术使用xamarine创建Android和IOS应用程序,我想在我的应用程序处于睡眠模式时发送推送通知, 这是我的方法

 public void pushNotifications(int count)
    {
        Notification.Builder builder = new Notification.Builder(this)
           .SetContentTitle("new Messages")
            .SetContentText("Hello World! This is my first notification!")
           .SetSmallIcon(Resource.Drawable.icon);

        // Instantiate the Inbox style:
        Notification.InboxStyle inboxStyle = new Notification.InboxStyle();

        // Set the title and text of the notification:
        builder.SetContentTitle(count+" new messages");
        //builder.SetContentText("chimchim@xamarin.com");


        // Plug this style into the builder:
        builder.SetStyle(inboxStyle);

        // Build the notification:
        Notification notification = builder.Build();

        // Get the notification manager:
        NotificationManager notificationManager =
            GetSystemService(Context.NotificationService) as NotificationManager;

        // Publish the notification:
        const int notificationId = 0;
        notificationManager.Notify(notificationId, notification);

    }

计数将来自我在便携式项目中编写的服务,当我尝试在可移植项目pushNotification中编写Notification.Builder方法时,我无法访问Notification命名空间。我希望每当应用程序进入sleepmode并使用portable显示计数时在pushNotification项目中触发一个方法,或者他们是否有其他方法可以执行此操作?

1 个答案:

答案 0 :(得分:0)

你应该尝试在OnSleep函数中的App.cs上调用你的方法:

public class App : Application  
{
    {
        ...
    }

    protected override void OnSleep ()
    {
        // Your method here
    }