2017-09-20 6 views
0

私は助けが必要です。私がタイトルで書いたように、データが到着してアプリケーションがバックグラウンドで殺されたり、殺されたりすると、通知からデータを保護する方法がわかりません。私が(下のコード)を渡す必要があるのは、私が精巧に必要なプレートです。コードは次のとおりです。Androidアプリケーションがバックグラウンドまたは閉鎖中にデータを通知から保護する方法

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

private static final String TAG = "MyFirebaseMsgService"; 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    String text= remoteMessage.getNotification().getBody(); 
    SharedPreferences s = getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE); 
    Functions.putStringInPrefs(s, Constants.PREFS_ALERT_PLATE, text); 


    if (Functions.isUserSignedUp(this)) { 

     if (remoteMessage.getNotification() != null) { 
      // Log.d(TAG, "Message Title:" + remoteMessage.getNotification().getTitle()); 

      Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
      try { 
       sendNotification(remoteMessage.getNotification()); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 


     } 


    } 
} 

private void sendNotification(RemoteMessage.Notification notification) { 
    Intent intent = new Intent(this, FragmentChangeActivity.class); 
    String prova = notification.getBody(); 



    intent.putExtra("alert_plate" , prova); 
    GlobalData.alertPlate=prova; // object used as cache 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    if(notification.getTitle()!= null){ 
     new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.details_icon).setContentTitle(notification.getTitle()); 

    } else{ 
     new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.details_icon).setContentTitle(getString(R.string.app_name)); 
    } 

    new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.details_icon).setContentText(notification.getBody()) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

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

    notificationManager.notify(new Random().nextInt((100000000 - 1) + 1) + 1, new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.details_icon).build()); 



    } 


} 

変更する必要がありますか?ありがとうございました ! Ps。マニフェストでは、クラスは正しい方法で宣言されます。アプリは、あなたがデータメッセージを使用してデータを送信する必要が近い場合には、データを受信する場合

答えて

0

はFCMメッセージ

1)通知メッセージ 2)データ・メッセージので

の2種類があります。あなたのデータフォーマットは以下のようになります

{ 
    "to": "device_id", 
    "data": { 
     "param_1": "vale 1", 
     "param_2": "value 2", 
     "param_3": "Value 3" 
    } 
} 

メッセージを受け取った後で、あなたは好きなものを共有環境設定やデータベースに保存することができます。

関連する問題