1

我刚刚添加聊天功能到我的应用程序后添加核心功能的应用程序。我开始实施Firebase推送通知到应用程序,并按照从官方文档的所有步骤。因此,每当我从Firebase通知发送消息控制台它表明,当应用程序在前台当应用在后台显示logcat的这些线,但没有通知Android后台通知不工作在Firebase

09-27 16:11:37.645 19946-19946/com.example.com.appD/dalvikvm: DexOpt: couldn't find field Landroid/os/Message;.sendingUid 
09-27 16:11:37.645 19946-19946/com.example.com.appW/dalvikvm: VFY: unable to resolve instance field 135 
09-27 16:11:37.645 19946-19946/com.example.com.appD/dalvikvm: VFY: replacing opcode 0x52 at 0x0000 

这里是我的火力地堡实例ID服务类

public class FireBase_InstanceID_Service extends FirebaseInstanceIdService { 
    public static final String TAG="==Firebase ID==="; 
    private static final String SubscribeTopic="Chat"; 
    String refreshedToken; 

    @Override 
    public void onTokenRefresh() { 
     super.onTokenRefresh(); 
     refreshedToken= FirebaseInstanceId.getInstance().getToken(); 
     Log.d(TAG,"Here Is Token "+refreshedToken); 
     FirebaseMessaging.getInstance().subscribeToTopic(SubscribeTopic); 

    } 
    private void sendRegistrationToServer(String Token){ 
     //TODO: Send Token To APP server 
    } 
} 

这里是我的火力地堡消息服务类

public class FireBase_Messaging_Service extends FirebaseMessagingService { 
     public static final String TAG="==FireBase MSG=="; 

     @Override 
     public void onMessageReceived(RemoteMessage remoteMessage) { 
      super.onMessageReceived(remoteMessage); 
      Log.d(TAG,"From "+remoteMessage.getFrom()); 

      if (remoteMessage.getData().size()>0){ 
       Log.d(TAG,"Message Data "+remoteMessage.getData()); 
      } 
      if (remoteMessage.getNotification()!=null){ 
       Log.d(TAG,"Message Notification Body "+remoteMessage.getNotification().getBody()); 
      } 
      Log.d(TAG,"FCM Message ID "+remoteMessage.getMessageId()); 
      Log.d(TAG,"FCM Notification Message: "+remoteMessage.getNotification()); 
      Log.d(TAG,"FCM Data Message "+remoteMessage.getData()); 
      showNotification(remoteMessage.getNotification().getBody()); 
     } 

     private void showNotification(String Message){ 
      Intent intent=new Intent(this, UserProfile.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      PendingIntent pendingIntent=PendingIntent.getActivity(this,5,intent,PendingIntent.FLAG_UPDATE_CURRENT); 

      NotificationCompat.Builder builder= (NotificationCompat.Builder) new NotificationCompat.Builder (this) 
        .setAutoCancel(true) 
        .setContentTitle("New Notification") 
        .setContentText(Message) 
        .setSmallIcon(R.drawable.common_google_signin_btn_icon_light_normal) 
        .setContentIntent(pendingIntent); 

      NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
      manager.notify(5,builder.build()); 

     } 
    } 

清单

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    package="com.example.com.app"> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".Navigation_Drawer" 
      android:theme="@style/AppTheme"> 

      <intent-filter> 
       <action android:name="android.intent.action.MAIN"></action> 
       <category android:name="android.intent.category.LAUNCHER"></category> 
      </intent-filter> 


     </activity> 

     <service 
      android:name=".ImageDownloading" 
      android:enabled="true" 
      android:exported="false"> 

     </service> 

<!--FIREBASE SERVICES --> 

     <service android:name=".FireBase_Messaging_Service"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT"></action> 
      </intent-filter> 
     </service> 

     <service android:name=".FireBase_InstanceID_Service"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT"></action> 
      </intent-filter> 
     </service> 

<!--FIREBASE SERVICES --> 

    </application> 

</manifest> 

所以对我来说,我无法获得推通知托盘通知在后台,或当应用程序被关闭有了这些条目logcat(我不明白)

+0

分享您的清单文件 –

+0

@AdnanAli请检查更新的问题 – androidXP

+0

检查此链接。 http://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase –

回答

0

这是工作只有当您的应用处于前台时,通知消息才会传递给您的onMessageReceived回调。如果您的应用程序处于后台或关闭状态,则会在通知中心显示通知消息,并且该消息中的任何数据都将传递到由于用户点击通知而启动的意图。

您可以指定click_action来指示用户点击通知时应启动的意图。如果未指定click_action,则使用主要活动。

当意图推出可以使用

getIntent().getExtras(); 

检索一组将包括与通知消息一起发送的任何数据。

有关通知消息的更多信息,请参阅文档。

+0

首先,我需要通知托盘中收到通知,然后处理数据。请再次阅读问题我没有收到应用程序关闭或后台通知 – androidXP