未收到FCM的解析服务器推送通知

时间:2017-11-15 13:51:21

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

我没有收到使用Parse Server的推送通知,我只是从仪表板上试了一下,它说" SENT"但它没有交付。

这是我的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

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。

首先,停止尝试直接使用Parse-Server进行推送通知。你不应该搞乱index.js文件。只需坚持使用FCM的firebase文档。

其次,请按照Firebase文档进行推送通知。

第三,准备就绪后,在必要时在您的Parse-Server中存储Firebase推送通知令牌。

从长远来看,这将节省您的资金,因为FCM是免费的。如果你干扰推动FC M与Parse-Server,它将耗尽你的动​​力。