我在FirebaseMessagingServices
上遇到了一些问题。
我有一个包含两个应用程序的项目,我们可以在其中将数据消息(带有有效负载的通知)从一个应用程序发送到另一个应用程序。当它们在前台时,它们在后台运行时工作正常,但是它们之间的间隔只有30分钟。
我的意思是说,如果该应用被杀死,则大约30分钟或一个小时后,它不会收到消息。我认为30分钟后他们将与Firebase
断开连接,我不希望这种情况发生。如果要断开连接,则它应重新连接到Firebase
本身,直到移动设备中可以连接互联网。
我做了很多研究,最终找到了BROADCAST
个接收器。即使该应用程序被杀死,它们也可以工作。因此,一旦断开连接,我如何使用Broadcast
接收器与Firebase
重新连接?
这是我的课程:
MyFirebaseMessaging
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
的帮助。
我想要的是,应用程序应该与FirebaseMessagingServices
或Whatsapp
或{{1 }}应用中,即使我几个小时前将其杀死,它们也会发送通知。
不是我只需要Uber
的帮助;如果还有其他方法,请与我分享。