【问题标题】:Parse Server Push notification with FCM not received未收到 FCM 解析服务器推送通知
【发布时间】:2017-11-15 13:51:20
【问题描述】:

我没有收到 Parse Server 的推送通知,我只尝试从仪表板显示“已发送”但未发送。

这是我的 index.js 配置:

  push: {
android: {
    senderId: '<my FCM sender Id>',
    apiKey: '<my FCM API Key>'
  }

这是我的 Manifest.xml:

 <uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />


<permission
    android:name="com.example.asus.shopper.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.asus.shopper.permission.C2D_MESSAGE" />

 <meta-data
        android:name="com.parse.push.gcm_sender_id"
        android:value="id:1196432287**" />

    <service android:name="com.parse.PushService" />



    <receiver
        android:name="com.parse.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="com.example.asus.shopper" />
        </intent-filter>
    </receiver>
    <receiver
        android:name="com.parse.ParsePushBroadcastReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="com.parse.push.intent.RECEIVE" />
            <action android:name="com.parse.push.intent.OPEN" />
            <action android:name="com.parse.push.intent.DELETE" />
        </intent-filter>
    </receiver>
      //here to test if notifications from firebase console is working and it's working
    <service android:name=".network.MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    <service android:name=".network.FirebaseIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>

    <meta-data android:name="com.parse.push.notification_icon"
        android:resource="@mipmap/ic_launcher"/>

    <receiver
        android:name=".network.CustomPushReceiver"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
            <action android:name="com.parse.push.intent.RECEIVE" />
            <action android:name="com.parse.push.intent.DELETE" />
            <action android:name="com.parse.push.intent.OPEN" />
        </intent-filter>
    </receiver>

这是我的自定义接收器的源代码

public class CustomPushReceiver extends ParsePushBroadcastReceiver {
private final String TAG = CustomPushReceiver.class.getSimpleName();

private NotificationUtils notificationUtils;

private Intent parseIntent;

public CustomPushReceiver() {
    super();
}

@Override
protected void onPushReceive(Context context, Intent intent) {
    super.onPushReceive(context, intent);

    if (intent == null){
        Log.d("CustomPushReceiver","null");
    }

    try {
        JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

        Log.e(TAG, "Push received: " + json);

        parseIntent = intent;

        parsePushJson(context, json);

    } catch (JSONException e) {
        Log.e(TAG, "Push message json exception: " + e.getMessage());
    }
}

@Override
protected void onPushDismiss(Context context, Intent intent) {
    super.onPushDismiss(context, intent);
}

@Override
protected void onPushOpen(Context context, Intent intent) {
    super.onPushOpen(context, intent);
}

/**
 * Parses the push notification json
 *
 * @param context
 * @param json
 */
private void parsePushJson(Context context, JSONObject json) {
    try {
        boolean isBackground = json.getBoolean("is_background");
        JSONObject data = json.getJSONObject("data");
        String title = data.getString("title");
        String message = data.getString("message");

        if (!isBackground) {
            Intent resultIntent = new Intent(context, DashboardActivity.class);
            showNotificationMessage(context, title, message, resultIntent);
        }

    } catch (JSONException e) {
        Log.e(TAG, "Push message json exception: " + e.getMessage());
    }
}


/**
 * Shows the notification message in the notification bar
 * If the app is in background, launches the app
 *
 * @param context
 * @param title
 * @param message
 * @param intent
 */
private void showNotificationMessage(Context context, String title, String message, Intent intent) {

    notificationUtils = new NotificationUtils(context);

    intent.putExtras(parseIntent.getExtras());

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    notificationUtils.showNotificationMessage(title, message, intent);
}

}

最后,这是我调用自定义接收器 DashboardActivity 的地方:

  @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    String message = intent.getStringExtra("message");

    Log.d("MESSAGE",message);
}

如果我错过了什么,请告诉我。我在 stackoverflow 中遵循了 Parse 文档和其他开发人员的答案。

PS:我使用的最低 sdk 版本是 19

【问题讨论】:

  • 嗨,你有解决方案吗?

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


【解决方案1】:

我遇到了同样的问题。

首先,停止尝试直接将 Parse-Server 用于推送通知。您不应该弄乱 index.js 文件。只需坚持 FCM 的 firebase 文档即可。

其次,按照 Firebase 文档获取推送通知。

第三,当您准备好后,将 Firebase 推送通知令牌存储在 Parse-Server 中的任何必要位置。

从长远来看,这将为您节省资金,因为 FCM 是免费的。如果您干扰推送 FC Mwith Parse-Server,它将耗尽您的测功机。

【讨论】:

  • 我正在使用解析 fcm 推送通知,我配置正确,所以我正在接收 fcm 推送,但没有从解析服务器接收,你知道问题出在哪里吗?如何解决这个问题?
  • 您好@Venkatesh,您找到解决方案了吗?谢谢
猜你喜欢
  • 1970-01-01
  • 2019-07-21
  • 2021-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-27
相关资源
最近更新 更多