2017-10-06 57 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

是的,我有同步服務來獲得計數,當它睡覺時,我應該打電話,並顯示計數的通知和同步服務是在解決方案的便攜式項目。 –

相關問題