2017-01-27 155 views
1

警告我在火力程序下面的代碼。收到通知時,如何顯示通知消息或數據作爲警報?我的服務器上我的PHP代碼通過ID成功推消息到Android設備和接收消息時,聽到的聲音,但我想顯示出警告或轉移到一個片段(我喜歡警報)。如何顯示從推送通知FCM

public class MyFirebaseMessagingService extends FirebaseMessagingService 
{ 
    private static final String TAG = "MyFirebaseMessService"; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) 
    { 
     commonfunc.myprint("#####_____MyFirebaseMessService_from 1 : " + remoteMessage.getFrom()); 


     if(remoteMessage.getData().size() > 0) 
     { 
      commonfunc.myprint("MyFirebaseMessService_getdata 2 : " + remoteMessage.getData()); 
      Map<String, String> data = remoteMessage.getData(); 
      String value1 = data.get("dtitle"); 
      String value2 = data.get("dbody"); 
      commonfunc.myprint("MyFirebaseMessService_getdata 2 : " + value1 + " " + value2); 
      sendNotification(remoteMessage.getData().toString()); 
     } 
     if (remoteMessage.getNotification() != null) 
     { 
      commonfunc.myprint("MyFirebaseMessService_getNot 3 body: " + remoteMessage.getNotification().getBody()); 
      commonfunc.myprint("MyFirebaseMessService_getNot 3 title: " + remoteMessage.getNotification().getTitle()); 

      sendNotification(remoteMessage.getNotification().getBody()); 
      sendNotification(remoteMessage.getNotification().getTitle()); 

      /* 
      final String mMessage = remoteMessage.getNotification().getBody(); 
      Handler h = new Handler(Looper.getMainLooper()); 
      h.post(new Runnable() { 
       public void run() 
       { 
        AlertDialog alertDialog = new AlertDialog.Builder(MyFirebaseMessagingService.this).create(); 
        alertDialog.setTitle("Message"); 
        alertDialog.setMessage("Message follows: " + mMessage); 
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int which) { 
            dialog.dismiss(); 
           } 
          }); 
        alertDialog.show(); 
        return ; 
       } 
      }); 
      */ 

     } 
    } 

    private void sendNotification(String body) 
    { 
     commonfunc.myprint("MyFirebaseMessService_sendNotification 4a sound "); 

     Intent intent = new Intent(this,MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT); 

     Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this) 
       .setAutoCancel(true) 
       .setContentTitle("Firebase Cloud Messaging") 
       .setContentText(body) 
       .setSound(notificationSound) 
       .setSmallIcon(life.poa.webcastman.poa1.R.drawable.common_google_signin_btn_icon_dark) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0,notifiBuilder.build()); 

     commonfunc.myprint("MyFirebaseMessService_sendNotification 4b sound"); 
     //Toast.makeText(getApplicationContext(), "____MyFirebaseMessagingService " + body, Toast.LENGTH_SHORT).show(); 

    } 

} 
+0

你裏面onMessageReceived獲取數據? –

+0

是的,下面是我的php - 並且 - 我得到通知和firebase例程中的數據並將它們寫入日誌$ json_array = [ 「notification」=> [ 「title」=>「nTEST」, 「sound 「=> 」默認「, 」身體「=> 」nollardata「 ], 」數據「=> [ 」dtitle「=> 」DTEST「, 」聲音「=> 」默認「, 」dbody「 => 「dollardata」 ], 「到」=> $目標, 「優先」=> 「高」 ]; $ body = json_encode($ json_array); –

+0

您的通知是否顯示數據? –

回答

2

使用本地廣播:將這個裏面onMessageReceived:

Intent intent = new Intent("myFunction"); 
       // add data 
       intent.putExtra("value1", value1); 
       intent.putExtra("value2", value2); 
       LocalBroadcastManager.getInstance(this).sendBroadcast(intent); 

在你活動/片段:

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      // Extract data included in the Intent 
      String t = intent.getStringExtra("value1"); 
      String t1 = intent.getStringExtra("value2"); 
      //alert data here 
     } 
    }; 


     @Override 
     public void onResume() { 
      super.onResume(); 
      LocalBroadcastManager.getInstance(this.getActivity()).registerReceiver(mMessageReceiver, 
        new IntentFilter("myFunction")); 
     } 

     @Override 
     public void onPause() { 
      super.onPause(); 
      LocalBroadcastManager.getInstance(this.getActivity()).unregisterReceiver(mMessageReceiver); 
     } 
+0

所以基本上把常規1在上的messageReceived功能...而且火力片段鈕私營廣播和我的功能是一個新的fragment..correct? –

+0

@BillFoyer Nope..Only在onMessageReceived的代碼第1部分和私營廣播在片段..'myFunction'只是一個名字!你可以給任何名字!這不是你的片段名稱 –