【问题标题】:Android FCM Push Notification not received when app is closed关闭应用程序时未收到 Android FCM 推送通知
【发布时间】:2017-04-11 10:29:58
【问题描述】:

我实现了 FCM 推送通知。主要问题是当应用程序关闭时,在设备上发生这种情况时通知不会到达。

我为此尝试了很多方法;当通知以“成功”发送服务器响应时,但我从未收到。

这是完整的代码。 Android、Manifest 和服务器。

安卓:

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.accionait.canje365.R;
import com.accionait.canje365.activities.ChatActivity;
import com.accionait.canje365.activities.HomeActivity;
import com.accionait.canje365.constants.Constants;
import com.google.firebase.messaging.RemoteMessage;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
    private static final String TAG = "FCM Service";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        showNotification(remoteMessage);
    }

    private void showNotification(RemoteMessage remoteMessage) {
        Uri uriDefaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
        .setSmallIcon(R.mipmap.ic_notification)
        .setLargeIcon((((BitmapDrawable)getResources().getDrawable(R.mipmap.ic_notification)).getBitmap()))
        .setContentTitle("TITLE")
        .setContentText("MESSAGE")
        .setContentInfo("0")
        .setAutoCancel(true)
        .setTicker("Canje365")
        .setSound(uriDefaultSound);

        Intent home = new Intent(this, HomeActivity.class);
        home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent intent = PendingIntent.getActivity(this, 0, home, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(intent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, builder.build());
    }
}

服务器:

var message = {
    to: token,
    data: {valor: 'test'}
};

fcm.send(message, function(err, response){  
    if(err) {
        console.log('Error: ' + err);
    } 
    else {
        console.log('Notificacion enviada: ', response);
    }
});

清单:

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

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

    <service android:name=".sync.RunIntentService" />
    <service android:name=".sync.FirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    <service android:name=".sync.FirebaseIdService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="MY_API_KEY" />

    <activity
        android:name=".activities.MainActivity"
        android:theme="@style/SplashScreenTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
         android:name=".activities.HomeActivity"
         android:label="@string/title_activity_login"
         android:screenOrientation="portrait">
    </activity>
</application>
</manifest>

怎么办?

【问题讨论】:

  • 我们很好,感谢您的问候。我猜您正在使用 Firebase 控制台发送通知,对吗?你必须实现自己的服务器,我知道,额外的工作,通过在 Java 中实现的相同的我,你也可以使用 PHP。阅读:stackoverflow.com/questions/37711082/…
  • 和官方文档解释消息的类型以及何时收到消息firebase.google.com/docs/cloud-messaging/concept-options
  • @Manny264 是我自己的服务器,不是 Firebase 控制台。我阅读了有关消息类型的文档。你可以看到我只发送数据消息来处理应用程序关闭时。我已经尝试过了。谢啦!如果您有其他建议,请告诉我。再次感谢。
  • @AL。是的,我使用 Postman 进行了测试。这是响应,但通知未到达设备。 { "multicast_id": 8973622958220667974, "success": 1, "failure": 0, "canonical_ids": 0, "results": [ { "message_id": "0:1480342544218725%8a20129df9fd7ecd" } ] } 我现在能做什么? ://
  • 嗨@AL。我找到了解决方案。我有一个华为,我有并且允许在后台运行。下面就是答案!谢谢大家!

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


【解决方案1】:

我遇到了同样的问题。这是许多手机型号的问题。我只是写这篇文章,这样如果有人遇到同样的问题,就可以少花一点时间搜索这个问题,因为我浪费了很多! Android M 及以上设备默认为所有应用启用电池优化,这通常会停止我们应用的后台服务。我们只需要要求用户禁用它们,如果用户选择不禁用它们,那么我很抱歉..我认为用户对您的应用程序功能并不满意。为了让用户启用它--->

Intent intent = new Intent();
        String packageName = getPackageName();
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (pm != null && pm.isIgnoringBatteryOptimizations(packageName))
                intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
            else {
                intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setData(Uri.parse("package:" + packageName));
            }
        }
        startActivity(intent);

【讨论】:

  • 你知道如何在 Ionic(一个混合移动应用程序开发平台)上使用它
【解决方案2】:

就我而言,我有一个华为 P8 Lite 并允许在后台运行。

这是您可以找到此案例详细信息的链接。

Solution on Huawei devices

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-10
    • 2021-06-13
    • 1970-01-01
    • 2016-10-17
    • 2017-12-02
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多