2

我將推送通知系統與Firebase集成到了我的項目中,並且運行良好。很少clarficiation使用WakefulBroadcastReceiver進行Firebase集成

要遵循的步驟火力地堡

整合
  1. 創建從火力控制檯JSON文件,並在我的項目增加(谷歌services.json)
  2. 依賴編譯的lib在我的構建添加。 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. 我檢查,如果從火力控制檯以及ARC(高級REST客戶端)發送了一個通知。我試過通知有效負載和數據有效負載都運行良好。

我的說明以及我們需要如何整合到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); 
    } 
    

    }

+0

您是否設法找到解決方案? 據我所知 '的FirebaseInstanceIdReceiver是接收FirebaseInstanceId和FirebaseMessaging事件,並將其傳送到您從FirebaseInstanceIdService.' – Poorya

回答

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:火力的消息:9.4.0' 應用插件:「com.google.gms .google-services'

Project Gradle classpath'com.google.gms:google-services:3.0.0'012 (「token_id」,FirebaseInstanceId.getInstance()。getToken());

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

    } 


} 
+0

派生類Refreshtoken不是一個問題,我沒有這樣只有WakefulBroadcastReceiver。但我的問題,我怎麼能通過使用WakefulBroadcastReceiver做同樣的整合?... – user2488539

+0

當應用程序從最新使用的應用程序中刪除時它是否會收到通知。在我的情況下,它不起作用。 – Gopal

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



    } 
} 
+1

這就是我目前所做的工作。但我的問題,我怎麼能通過使用WakefulBroadcastReceiver做同樣的整合?... – user2488539

0

請在其它設備進行檢查。因爲某些設備不允許應用程序通過更改設置在後臺運行。因此,Firebase服務已停用。

我面對這個問題,這就是爲什麼我與你分享。