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

时间:2017-06-23 07:20:34

标签: android firebase push-notification firebase-cloud-messaging

我知道在stackoverflow上几乎没有类似的问题。但是,答案要么不是我想要的,要么是GCM而不是FCM。

现在,当应用程序处于前台或后台时,我可以同时调用onMessageReceived。但我想要的是在应用程序关闭或从任务管理器上滑过时调用,而不是强制关闭。即使应用已关闭,我也能够收到推送通知,但我想将其称为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>

1 个答案:

答案 0 :(得分:0)

当应用程序关闭时,

关于按摩接收将不会调用它只会在APP启用时调用,因此您必须处理必须在mainfiest中重定向的类,并将操作放入服务器的支付中并获取所有数据用户点击通知使用捆绑。 在mainfiest中使用action这样在服务器脚本或cade中给出任何名称但同名。

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

服务器中的

在您的有效负载或json请求中单击操作,如此

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

点击通知时点击与主要活动相同的操作,当您点击通知时,它将在主要的最佳动作中重新启动为maithin活动。

获取通知并保存在重定向的活动中

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