【问题标题】:Firebase Integration using WakefulBroadcastReceiver使用 WakefulBroadcastReceiver 的 Firebase 集成
【发布时间】:2017-02-16 13:11:17
【问题描述】:

我将推送通知系统与 Firebase 集成到我的项目中,并且运行良好。一点澄清

集成 Firebase 的步骤

  1. 从 firebase 控制台创建 json 文件并添加到我的项目 (google-services.json)
  2. 在我的 build.gradel 中添加了依赖编译库(根以及应用程序内部)

  3. AndroidManifest.xml 添加了以下服务器。

    <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>
    
  4. 在“myfirebasemsgservice”内部添加了以下行,并且运行良好。

    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());
    
  5. 如果从 Firebase 控制台和 ARC(高级 REST 客户端)发送,我检查并收到通知。我试过 Notification Payload 和 Data Payload 都运行良好。

我的说明以及我们需要如何集成到 firebase 与下面的代码相同。我们以前是怎么做的意思?任何想法以及我们需要如何像这样集成?

  1. AndroidManifest.xml - 之前添加。

                      <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" />
    
  2. gcmbroadcastReceiver 添加了以下代码。

          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)));
         }
         }
    
  3. gcmIntentService 添加了以下代码。

               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);
    }
    

    }

【问题讨论】:

  • 您找到解决方案了吗?据我所知The FirebaseInstanceIdReceiver is a WakefulBroadcastReceiver that receives FirebaseInstanceId and FirebaseMessaging events and delivers them to the class that you derive from FirebaseInstanceIdService.

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


【解决方案1】:
 <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>

应用分级

编译“com.google.firebase:firebase-messaging:9.4.0” 应用插件:'com.google.gms.google-services'

项目 Gradle 类路径 'com.google.gms:google-services:3.0.0'

Log.e("token_id", FirebaseInstanceId.getInstance().getToken());

【讨论】:

    【解决方案2】:
         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) {
    
        }
    
    
    }
    

    【讨论】:

    • Refreshtoken 不是问题,我只是喜欢这个。但我的问题是如何使用 WakefulBroadcastReceiver 进行相同的集成?...
    • 从最新使用的应用程序中删除应用程序时是否收到通知。就我而言,它不起作用。
    【解决方案3】:
        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());
    
    
    
        }
    }
    

    【讨论】:

    • 这是我目前所做的并且运行良好。但我的问题是如何使用 WakefulBroadcastReceiver 进行相同的集成?...
    【解决方案4】:

    请检查其他设备。因为某些设备不允许应用程序通过更改设置在后台运行。因此 Firebase 服务被禁用。

    我遇到了这个问题,这就是我与你分享的原因。

    【讨论】:

      猜你喜欢
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 2019-02-18
      • 2019-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多