【问题标题】:Android Background Notification Not working In FirebaseAndroid背景通知在Firebase中不起作用
【发布时间】:2016-09-27 11:20:35
【问题描述】:

我只是在添加应用程序的核心功能后将聊天功能添加到我的应用程序中。我开始在应用程序中实现 Firebase 推送通知并按照官方文档中的所有步骤进行操作。因此,每当我从 Firebase 通知控制台发送消息时,它都会显示应用程序在 foreground 但是当应用程序在 background 中时,它会在 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 消息服务类

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

【问题讨论】:

  • 分享你的清单文件
  • @AdnanAli 请检查更新的问题
  • @AdnanAli 我只能在通知托盘中收到通知时才能处理通知,而当应用程序在后台或关闭时我没有收到通知
  • 尝试登录 onMessageReceived 检查方法没有被调用或通知没有显示

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


【解决方案1】:

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

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

Intent 启动后,您可以使用

getIntent().getExtras();

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

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

【讨论】:

  • 首先我需要在通知托盘中获得通知,然后处理数据。请再次阅读问题,当应用程序关闭或在后台时我没有收到通知
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-05
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-29
相关资源
最近更新 更多