2017-10-06 56 views
0

我正在使用CrossPlateForm技术使用xamarine创建Android和IOS应用程序,我想在应用程序处于睡眠模式时发送推送通知, this是我的方法如何仅在应用程序处于睡眠模式时从便携式项目调用pushNotification

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("[email protected]"); 


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

    } 

计数将来自我在便携式项目写道,当我试图写在便携式项目Notification.Builder我无法访问Notification命名空间pushNotification方法的服务。我想在portable项目中触发一个方法,只要应用程序使用睡眠模式并使用pushNotification显示计数,或者是他们的任何替代方式来执行此操作?

+0

你是什么意思“睡眠模式”?是[this](https://android.stackexchange.com/questions/95625/what-is-androids-sleep-mode)? –

回答

0

你应该尝试调用OnSleep函数内部上App.cs方法:

public class App : Application 
{ 
    { 
     ... 
    } 

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

但在此之前,我需要调用我的服务来获取消息的数量,然后我应该调用notificatin方法,那么如何从这里调用我的服务? –

+0

您的代码@SyedRasheed中是否有某种同步服务类? – FabriBertani

+0

是的,我有同步服务来获得计数,当它睡觉时,我应该打电话,并显示计数的通知和同步服务是在解决方案的便携式项目。 –

相关问题