首页 > 解决方案 > 将 BroadcastReceiver 添加到 FirebaseMessagingServices

问题描述

我遇到了一些问题FirebaseMessagingServices

我有一个包含两个应用程序的项目,我们可以将数据消息(带有有效负载的通知)从一个应用程序发送到另一个应用程序。他们在前台工作正常,当他们在后台时,他们会收到消息,但间隔只有 30 分钟。

我的意思是如果应用程序被杀死,大约 30 分钟或一个小时后它不会收到消息。我认为他们会在 30 分钟后断开连接Firebase,我不希望这种情况发生。如果它正在断开连接,那么它应该重新连接到Firebase自己,直到手机中有互联网连接。

我研究了很多,终于找到了BROADCAST接收器。即使应用程序被杀死,它们也能工作。那么,一旦它们断开连接,我如何使用Broadcast接收器重新连接?Firebase

这是我的课程:

MyFirebase 消息传递

public class MyFirebaseMessaging extends FirebaseMessagingService {

@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {

    if (remoteMessage.getData() != null) {
        Map<String, String> data = remoteMessage.getData();
        String title = data.get("title");
        final String companyName = data.get("CompanyName");
        final String BookingIdC = data.get("BookingIdC");
        final String BookingIdT = data.get("BookingIdT");
        final String companyPhone = data.get("CompanyPhone");
        final String companyRates = data.get("CompanyRates");
        final String companyId = data.get("CompanyId");
        final String Date = data.get("Date");
        final String companyIdC = data.get("companyIdC");
        final String Time = data.get("Time");
        final String Id = data.get("Id");
        final String Address = data.get("Address");
        final String Bookingid = data.get("Bookingid");
        final String TimeCB = data.get("TimeCB");
        final String DateCB = data.get("DateCB");
        final String EventType = data.get("EventType");
        final String messageCB = data.get("messageCB");
        final String AddressCB = data.get("AddressCB");
        final String companythatcancelledthebooking = data.get("CompanyNamethatcancelledthebooking");

        final String message = data.get("message");
        //  remoteMessage.getNotification().getTitle() = title and  remoteMessage.getNotification().getBody() = message
        if (title != null && title.equals("Cancel")) {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(MyFirebaseMessaging.this, DeclinedWindow.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);

                    Common.isCompanyFound = false;
                    Common.companyId = "";
                    Toast.makeText(MyFirebaseMessaging.this, "" + message, Toast.LENGTH_SHORT).show();
                }
            });
        } else if (title != null && title.equals("cancelAdvanceBooking")) {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(getBaseContext(), CompanycancelledtheBooking.class);
                    intent.putExtra("DateCB", DateCB);
                    intent.putExtra("TimeCB", TimeCB);
                    intent.putExtra("messageCB", messageCB);
                    intent.putExtra("AddressCB", AddressCB);
                    intent.putExtra("EventType", EventType);
                    intent.putExtra("Id", Id);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    startActivity(intent);

                    Common.isCompanyFound = false;
                    Common.companyId = "";
                    Toast.makeText(MyFirebaseMessaging.this, "" + messageCB, Toast.LENGTH_SHORT).show();
                }
            });

        } else if (title != null && title.equals("Accept")) {


            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(MyFirebaseMessaging.this, AcceptedWindow.class);
                    intent.putExtra("Date", Date);
                    intent.putExtra("Time", Time);
                    intent.putExtra("Address", Address);
                    intent.putExtra("companyName", companyName);
                    intent.putExtra("companyPhone", companyPhone);
                    intent.putExtra("companyRates", companyRates);
                    intent.putExtra("companyId", companyId);
                    intent.putExtra("Bookingid", Bookingid);
                    intent.putExtra("EventType", EventType);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    startActivity(intent);
                    Common.isCompanyFound = false;
                    Common.companyId = "";


                    Toast.makeText(MyFirebaseMessaging.this, "" + message, Toast.LENGTH_SHORT).show();
                }
            });

        } else if (title != null && title.equals("Arrived")) {
            Handler handler = new Handler(Looper.getMainLooper());

            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                        showArrivedNotifAPI26(message);
                    else
                        showArrivedNotif(message);

                }
            });
        } else if (title != null && title.equals("Completed")) {

            openRateactivity(message);
        } else if (title != null && title.equals("completedAdvancebooking")) {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(MyFirebaseMessaging.this, VerifyingCompletedBooking.class);
                    intent.putExtra("BookingIdC", BookingIdC);
                    intent.putExtra("message", message);
                    intent.putExtra("companyid", companyIdC);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    startActivity(intent);

                }
            });
        } else if (title != null && title.equals("Ontheway")) {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(MyFirebaseMessaging.this, Onthewayandimreached.class);
                    intent.putExtra("message", message);
                    intent.putExtra("BookingIdT", BookingIdT);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    startActivity(intent);

                }
            });
        } else if (title != null && title.equals("Reached")) {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(MyFirebaseMessaging.this, Onthewayandimreached.class);
                    intent.putExtra("message", message);
                    intent.putExtra("BookingIdT", BookingIdT);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    startActivity(intent);

                }
            });
        }

    }
}

@RequiresApi(api = Build.VERSION_CODES.O)
private void showArrivedNotifAPI26(String body) {
    PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 0,
            new Intent(), PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationHelper notificationHelper = new NotificationHelper(getBaseContext());
    Notification.Builder builder = notificationHelper.getUberNotification("Arrived", body, contentIntent, defaultSound);
    notificationHelper.getManager().notify(1, builder.build());

}

private void openRateactivity(String body) {

    Intent intent = new Intent(this, RateActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

private void showArrivedNotif(String body) {
    PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 0,
            new Intent(), PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext());
    builder.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
            .setWhen(System.currentTimeMillis()).
            setSmallIcon(R.drawable.ic_menu_camera)
            .setContentTitle("Arrived")
            .setContentText(body)
            .setContentIntent(contentIntent);

    NotificationManager manager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(1, builder.build());
}

MyFirebaseIdService

    public class MyFirebaseIdService extends FirebaseInstanceIdService {
        @Override
        public void onTokenRefresh() {
            super.onTokenRefresh();

            String refreshedToken = FirebaseInstanceId.getInstance().getToken();
            updateTokenToServer(refreshedToken);

        }

        private void updateTokenToServer(String refreshedToken) {

            FirebaseDatabase db = FirebaseDatabase.getInstance();
            DatabaseReference tokens = db.getReference(Common.token_table);

            Token token = new Token(refreshedToken);
            if (FirebaseAuth.getInstance().getCurrentUser() != null)
                tokens.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(token);

        }
    }

AndroidManifest

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.praful.ubercoustomer">

    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality. 
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
    <uses-permission android:name="android.permission.INTERNET" />

    <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="com.facebook.accountkit.ApplicationName"
            android:value="@string/app_name" />
        <meta-data xmlns:tools="http://schemas.android.com/tools"
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/FACEBOOK_APP_ID"
            tools:replace="android:value" />
        <meta-data
            android:name="com.facebook.accountkit.ClientToken"
            android:value="@string/ACCOUNT_KIT_CLIENT_TOKEN" />

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity
            android:name=".Home"
            android:label="@string/title_activity_home"
            android:theme="@style/AppTheme" />

        <service
            android:name=".Service.MyFirebaseIdService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
        <service
            android:name=".Service.MyFirebaseMessaging"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <activity android:name=".RateActivity" />
        <activity android:name=".CallCompany" />
        <activity
            android:name=".DeclinedWindow"
            android:theme="@style/AppTheme" />
        <activity
            android:name=".AcceptedWindow"
            android:theme="@style/AppTheme" />
        <activity
            android:name=".AdvanceBookings"
            android:parentActivityName=".Home"
            android:theme="@style/AppTheme" />
        <activity android:name=".CompanyInfo" />
        <activity android:name=".AdvanceBookingsDetails" />
        <activity
            android:name=".CompanycancelledtheBooking"
            android:theme="@style/AppTheme" />
        <activity
            android:name=".CancelledBookings"
            android:parentActivityName=".Home"
            android:theme="@style/AppTheme" />
        <activity android:name=".VerifyingCompletedBooking" />
        <activity
            android:name=".CompletedBookings"
            android:parentActivityName=".Home"
            android:theme="@style/AppTheme" />
        <activity
            android:name=".Onthewayandimreached"
            android:theme="@style/AppTheme" />
        <activity
            android:name=".RateactivityforAdvanceBooking"
            android:theme="@style/AppTheme" />
        <activity
            android:name=".CallCompanyforAdvanceBooking"
            android:label="@string/title_activity_call_companyfor_advance_booking"
            android:theme="@style/AppTheme" />
        <activity
            android:name=".Howtouse"
            android:parentActivityName=".Home"
            android:theme="@style/AppTheme" />
        <activity android:name=".Listofavailablecompanies" />
        <activity android:name=".Availablecompanydetails"></activity>
    </application>

</manifest>

我需要帮助BroadcastReciever在服务和活动之间实现(FirebaseMesssaging服务 --> Broadcastreceiver--> Activity)。

我想要的是,应用程序应该连接到FirebaseMessagingServices24/7(即使应用程序被杀死并死了几个小时),就像WhatsappUberyoutube应用程序一样,即使我在几个小时前杀死它们,它们也会发送通知。

并不是说我只需要帮助Broadcasts;如果有任何其他方法可以做到这一点,请与我分享。

标签: androidfirebasefirebase-cloud-messagingandroid-broadcastreceiver

解决方案


推荐阅读