2017-06-21 22 views
0

我有一款老年人使用的應用程序,我需要一種非常明顯的方式讓他們知道他們收到了新通知,所以我想要顯示一個對話框中的屏幕,即使應用程序可能會被關閉/背景當收到Firebase通知並且App在後臺時顯示對話框

我創建的擴展對話框顯示一個對話框類,它工作時,我把它在我的任何活動:

錯誤有道理因爲FirebaseMessagingService實際上不是我的課程之一,但我不知道如何解決此問題,輸入讚賞

感謝

+0

你在這裏要做的是繪製其他應用程序。爲此,您需要SYSTEM_ALERT_WINDOW,它需要在系統設置中顯式啓用API23 +。這是一個巨大的安全責任,你應該重新考慮這是否是真正需要的。更多閱讀:https://stackoverflow.com/a/32652625/1025599。作爲替代,您可以使用'NotificationManager'來顯示標準通知。 – Nachi

+0

恕我直言,在通知到達時出現對話框*彈出*是壞的UX *。這就像[彈出式廣告](https://en.wikipedia.org/wiki/Pop-up_ad)。 –

+0

這是,但我的應用程序是用於老年人,以幫助他們的健康常規,他們並不認爲他們有一個新的活動,除非你讓它真的引人注目。這種做法有一個正確的用法。 – dfabiano

回答

1

創建類延伸FirebaseMessagingService寫onMessageReceived方法,從中可以顯示通知對話框,或者你喜歡做什麼都喜歡:

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

@Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     } 
} 
+0

這就是我所擁有的,但「alert.showNotificationDialog(FirebaseMessagingService.this」這一行給出了一個錯誤,如果我做了「myclassname.this」,它也會這樣做。 – dfabiano

+0

也許問題是我的shownotificationDialog函數需要一個活動以顯示活動在...但是,當活動在後臺時,我通過什麼活動來顯示對話框?只是猜測我不知道這是否是實際問題 – dfabiano

+0

如果要處理推送在對話框中的通知消息比你必須使用廣播接收器。 – ashish

0
  1. 創建MyFirebaseMessagingService extends FirebaseMessagingService 其在入門AndroidManifest和方法onMessageReceived

  2. 發送數據信息(不是通知/顯示信息)。

  3. 使用context.startActivity()發起一個活動

  4. 定製活動的風格看起來像一個對話框

見:Android Activity as a dialog

0

我可能有點但是這裏有一個完整的解決方案,即使應用程序關閉或死亡,或者在後臺強制活動自動打開。該解決方案甚至可以在設備處於睡眠或屏幕鎖定狀態時顯示「活動/片段」。

創建一個活動,它將爲通知提供服務,並在收到通知時自動打開。

裏面你

onMessageReceived

執行以下代碼

if (remoteMessage.getData().size() > 0) { 
sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body")); 
} 

這裏您sendNotification的()方法是

private void sendNotification(String messageTitle,String messageBody) { 
    Intent intent = new Intent(this, DeliveryRecieved.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* request code */, intent,PendingIntent.FLAG_UPDATE_CURRENT); 

    long[] pattern = {500,500,500,500,500}; 

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); 

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_dart_board) 
      .setContentTitle(messageTitle) 
      .setContentText(messageBody) 
      .setVibrate(pattern) 
      .setLights(Color.BLUE,1,1) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
    getApplicationContext().startActivity(intent); 
} 

件注意兩個重要的事情

FLAG_ACTIVITY_NEW_TASK 和 getApplicationContext()。startActivity(意向);

在您的活動,如果你想取消鎖定設備屏幕和設備休眠模式使應用程序回來做你的活動以下的onCreate()

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON); 

的最後一個重要的事情是在firebase消息傳遞系統總是使用「數據有效載荷」,因爲當設備在後臺時調用onMessageReceived()「需要數據有效載荷」

要顯示任何對話框,您可以在您的活動的onCreateView()或任何位置編碼它;

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    final DeliveryRecieved c = this; 
    setContentView(R.layout.activity_jp_map); 
    mediaPlayer = MediaPlayer.create(this, R.raw.call_bell); 
    mediaPlayer.setLooping(true); 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON); 
     String[] title = {"A","B"}; 
     mediaPlayer.start(); 
     new MaterialDialog.Builder(this) 
       .items(title) 
       .itemsCallbackMultiChoice(null, new MaterialDialog.ListCallbackMultiChoice() { 
        @Override 
        public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) { 

         mediaPlayer.stop(); 
         c.finish(); 
         return true; 
        } 
       }) 
       .title("NOTICES For Delivery") 
       .content("IMPORTANT NOTICE") 
       .positiveText("Accept") 
       .build().show(); 


    } 
相關問題