我知道在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());
}
}
主活动.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>
在按摩接收时不会在应用程序关闭时调用,它只会在应用程序打开时调用,因此您必须处理您必须在维护中重定向哪个类并在服务器中的支付中执行操作,并获取所有数据,用户单击使用捆绑包通知。 像这样在 mainfest 中使用操作,在服务器脚本或 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'
);
单击与维护活动操作相同的 Antion 当您单击通知时,它将重新转换为活动 在维护操作中是 maithin 。
获取通知并保存在重定向活动中
Intent intent = getIntent();
if (intent != null && intent.getExtras() != null) {
Bundle extras = intent.getExtras();
String someData= extras.getString("data");
String someData2 = extras.getString("data");