使用WakefulBroadcastReceiver进行Firebase集成

时间:2017-02-16 13:11:18

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

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

跟踪Firebase集成的步骤

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

  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 CLIENT)发送,我检查并收到通知。我尝试过Notification Payload和Data Payload都运行良好。

  6. 我的澄清以及我们如何在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);
      }
      

      }

4 个答案:

答案 0 :(得分: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>

App Gradle

编译'com.google.firebase:firebase-messaging:9.4.0'  申请插件:'com.google.gms.google-services'

Project Gradle    classpath'com.google.gms:google-services:3.0.0'

Log.e(“token_id”,FirebaseInstanceId.getInstance()。getToken());

答案 1 :(得分:1)

     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) {

    }


}

答案 2 :(得分:1)

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



    }
}

答案 3 :(得分:0)

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

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