即使关闭应用程序(FCM)时如何调用onMessageReceived?

问题描述:

我知道在*上很少有类似的问题。但是,答案要么不是我要找的,要么是针对GCM而不是FCM。即使关闭应用程序(FCM)时如何调用onMessageReceived?

现在,我可以在应用程序处于前景或背景时拨打onMessageReceived。但我想要的是在应用程序关闭或从任务管理器中移除时调用,而不是FORCE CLOSE。即使应用已关闭,我也能收到推送通知,但我想将其称为onMessageReceived,因为我将推送通知保存在Firebase上。

所以问题是我无法保存/写入数据(推送通知)到应用程序关闭时,即使我收到它的Firebase。我想在应用程序关闭时保存推送通知。我可以在应用程序关闭时使用onMessageReceived或者我可以使用其他方法吗?我将发布下面的代码,以便更容易理解我在做什么。

MyFirebaseMessagingService.java

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

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
createNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body")); 


     Bundle bundle = new Bundle(); 
     bundle.putString("msgBody", remoteMessage.getData().get("body")); 
     bundle.putString("time", currentDateTimeString); 
     Intent new_intent = new Intent(); 
     new_intent.setAction("ACTION_STRING_ACTIVITY"); 
     new_intent.putExtra("msg", bundle); 

     sendBroadcast(new_intent); 

     //create notification 
//  createNotification(remoteMessage.getNotification().getBody()); 

    } 


    private void createNotification(String messageTitle, String messageBody) { 
     Intent intent = new Intent(this , MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent resultIntent = PendingIntent.getActivity(this , 0, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle(messageTitle) 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(notificationSoundURI) 
       .setContentIntent(resultIntent); 

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

     notificationManager.notify(0, mNotificationBuilder.build()); 
    } 

} 

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     setContentView(R.layout.menu_actionbar_tab); 

activityReceiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       Bundle bundle = intent.getBundleExtra("msg"); 
       String body = bundle.getString("msgBody"); 
       FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); 
       String userID = user.getUid(); 
       Log.d("body", body); 
       mRef.child("customers").child(userID).child("Notification").push().setValue(body); 


     if (activityReceiver != null) { 
      IntentFilter intentFilter = new IntentFilter("ACTION_STRING_ACTIVITY"); 
      registerReceiver(activityReceiver, intentFilter); 
     } 
} 

舱单

<application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <meta-data 
      android:name="android.support.VERSION" 
      android:value="25.3.0 " /> 

     <meta-data 
      android:name="com.google.android.geo.API_KEY" 
      android:value="@string/google_maps_key" /> 

     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 

     <activity 
      android:name=".Login.ChooseLoginMethodActivity" 
      android:screenOrientation="portrait"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

    <service android:name=".Notification.MyFirebaseMessagingService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
      </intent-filter> 
     </service> 

    </application> 
+0

能否请您出示货单代码 – Ancee

+0

@Ancee我包括我的清单代码。 – arsenallavigne

+0

请注意,如果您希望在后台应用程序中调用onMessageReceived方法,您的'FCM'消息必须不包含'notification'键(只能使用'data'键)。 – NamNH

按摩接收不会在应用程序关闭时调用,它只会在应用程序处于打开状态时调用,因此您必须处理要在mainfiest中重定向的哪个类,并将操作放在服务器中并获取所有数据用户单击使用包的通知。 在mainfiest中使用动作就像这样在你的服务器脚本或cade中给出任何名字,但同名。

<activity 
      android:name="your activity name"> 
      <intent-filter> 
       <action android:name="abc" /> 
       <category android:name="android.intent.category.DEFAULT"/> 
      </intent-filter> 

在服务器化妆点击动作在你有效载荷或类似这样的

notificatiin = array 
(
'icon' => 'any', 
'title' => 'any', 
'body' => 'massage', 
    'click_action' => 'abc' 
); 

点击antion一样mainfiest活动的行动,当您单击通知将redect到活动至极JSON请求maithin在最温和的蒙山行动。

GET通知,并保存在重定向活动

Intent intent = getIntent(); 
if (intent != null && intent.getExtras() != null) { 
Bundle extras = intent.getExtras(); 
String someData= extras.getString("data"); 
String someData2 = extras.getString("data");