Android 使用WakefulBroadcastReceiver的Firebase集成
在我的项目中,我将推送通知系统与Firebase集成在一起,它运行良好。很少澄清 遵循Firebase集成的步骤Android 使用WakefulBroadcastReceiver的Firebase集成,android,firebase,push-notification,google-cloud-messaging,firebase-cloud-messaging,Android,Firebase,Push Notification,Google Cloud Messaging,Firebase Cloud Messaging,在我的项目中,我将推送通知系统与Firebase集成在一起,它运行良好。很少澄清 遵循Firebase集成的步骤 从firebase控制台创建json文件并添加到我的项目中(google-services.json) 在my build.gradel中添加的依赖项编译库(根目录以及应用程序内部) AndroidManifest.xml添加了以下服务器 <service android:name="com.myfirebase.myfirebasemsgservice" android:en
<service
android:name="com.myfirebase.myfirebasemsgservice"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
public void onMessageReceived(RemoteMessage fcmMessage)
{
Log.d(TAG, "From: " + fcmMessage.getFrom());
if (fcmMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + fcmMessage.getData());
Log.d(TAG, "Message data payload: " + fcmMessage.getData().toString());
我的澄清以及我们需要如何在firebase中集成,如下代码所示。我们以前是怎么做的?。有什么想法以及我们需要如何像这样集成?
<receiver
android:name="com.mygcm.gcmbroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<service android:name="com.mygcm.gcmIntentService" />
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GCMIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
}
}
public class gcmIntentService extends IntentService {
@Override
protected void onHandleIntent(Intent intent)
{
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
try
{
if (!extras.isEmpty())
{
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType))
{
Log.d("LogData",intent.getStringExtra(Constants.PAYLOAD));
}
else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType))
{
Log.d("LogData",intent.getStringExtra(Constants.PAYLOAD));
}
else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType))
{
Log.d("LogData",intent.getStringExtra(Constants.PAYLOAD));
}
}
}
catch(Exception ignored)
{
ExceptionTrack exe_track=ExceptionTrack.getInstance();
}
finally {
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
}
应用程序Gradle
编译'com.google.firebase:firebase消息:9.4.0'
应用插件:“com.google.gms.googleservices”
格拉德尔项目
classpath'com.google.gms:googleservices:3.0.0'
Log.e(“token_id”,FirebaseInstanceId.getInstance().getToken()) 请签入其他设备。因为某些设备不允许通过更改设置在后台运行应用程序。因此,Firebase服务被禁用
我面临着这个问题,这就是我与你分享的原因。这就是我目前所做的,并且工作得很好。但我的问题是,如何使用WakefulBroadcastReceiver进行相同的集成?…Refreshtoken不是一个问题,我只是这样做的。但我的问题是,如何使用WakefulBroadcastReceiver进行相同的集成?…当应用程序从使用的最新应用程序中删除时,它是否会收到通知。就我而言,它不起作用。你找到解决办法了吗?据我所知,FirebaseInstancedReceiver是一个WakefulBroadcastReceiver,它接收FirebaseInstanceId和FirebaseMessaging事件,并将它们传递给从FirebaseInstancedService派生的类。
<service
android:name=".fcm.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".fcm.FireBaseInstanceID_Service">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
import com.loyalty.preferences.LoyaltySharedpreference;
public class FireBaseInstanceID_Service extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
//Getting registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
//Displaying token on logcat
}
private void sendRegistrationToServer(String token) {
}
}
package com.example.fcm;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody());
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo2)
.setContentTitle(" Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}