Android背景通知在Firebase

时间:2016-09-27 11:20:36

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

我只是在添加apps的核心功能后将聊天功能添加到我的应用程序中。我开始在应用程序中实施Firebase推送通知并按照官方文档中的所有步骤进行操作。因此,每当我从Firebase通知控制台发送消息时,它都显示应用程序在< strong>前景,但当后台中的应用时,它会在logcat中显示这些行但没有通知

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

以下是我的Firebase实例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
    }
}

这是我的Firebase Messaging Service类

 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中的这些条目(我不明白)

1 个答案:

答案 0 :(得分:0)

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

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

启动意图后,您可以使用

policy based authentication, Shield

检索包含与通知消息一起发送的任何数据的Set。

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