2017-04-18 59 views
1

我一直在使用我正在構建的應用程序中的通知。構建通知的方式已經發生了很大變化,因爲它看起來很老,但最終我設法顯示了一個通知。 (*)手機在睡覺時在Android中發出通知

雖然我有兩個問題: 1)通知沒有聲音。這不是增加平板電腦上的通知量的問題,因爲例如來自gmail的通知確實會產生聲音。如何發出聲音和振動通知? (我正在嘗試在Android 6中使用)

2)更重要的是,即使手機正在睡眠時,如何發出「通知」我的通知?到目前爲止,我的通知發生在手機正在睡覺時,但我必須打開手機才能看到它們。理想情況下,這些應該出現即使當屏幕是黑色的

編輯:只是要清楚,通知在手機正在睡覺時發生。只是我沒有看到他們,除非我打開屏幕,他們是。我想對於通知「在屏幕上打開」

(*)我當前的代碼是

  NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 

     PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(), 0); 


     Notification notification= new Notification.Builder(context) 
       .setContentTitle(message) 
       .setContentText("Proximity Alert!") 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setAutoCancel(true) 
       .setContentIntent(pi).build(); //getNotification(); 


     //notification.flags= Notification.FLAG_AUTO_CANCEL; 

     //notification.setLatestEventInfo(context, "GAST", "Proximity Alert", pi); //this is deprecated 

     notificationManager.notify(NOTIFICATION_ID, notification); 
+1

聲音:URI defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);並在通知生成器:.setSound(defaultSoundUri) –

回答

1

是的,你可以同時用這種方式發出通知補充完善。

NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICAT‌​ION); 
builder.setSound(defaultSoundUri); 

而要做到這一點,而睡覺的手機,你將不得不使用服務。

文檔:Services

+0

謝謝你的答覆。關於第二項,我可能沒有很好地解釋自己,對不起。通知**在睡覺時確實發生**。如果我按下按鈕,我可以清楚地看到他們甚至沒有使用服務。我的問題是我想讓他們喚醒電話。我怎樣才能做到這一點? – KansaiRobot

+0

請參閱此喚醒屏幕 [鏈接](http://stackoverflow.com/questions/13977448/wake-up-android-phone-tablet) 希望這可以幫助 –