简体   繁体   English

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

[英]Parse Server Push notification with FCM not received

I don't receive Push notification with Parse Server, i tried only from dashboard it says "SENT" but it's not delivered.我没有收到 Parse Server 的推送通知,我只尝试从显示“已发送”的仪表板发送,但未发送。

Here's my index.js configurations:这是我的 index.js 配置:

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

here's my Manifest.xml:这是我的 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>

Here is my custom receiver's source code这是我的自定义接收器的源代码

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);
}

} }

Finally, this is where i call the custom receiver DashboardActivity:最后,这是我调用自定义接收器 DashboardActivity 的地方:

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

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

Please tell me if i miss a thing.如果我错过了什么,请告诉我。 I followed the Parse documentation and other's developpers answers in stackoverflow with no lack.我在 stackoverflow 中遵循了 Parse 文档和其他开发人员的回答,不乏其人。

PS: the minimum sdk version i'm using is 19 PS:我使用的最低 sdk 版本是 19

I had the same problem. 我有同样的问题。

First, stop trying to use Parse-Server directly for Push Notifications. 首先,停止尝试直接将Parse-Server用于推送通知。 You shouldn't be messing the with the index.js file. 您不应该将index.js文件弄乱。 Just stick to the firebase docs for FCM. 只需坚持使用FCM的firebase文档即可。

Second, Follow the Firebase docs for push notifications. 其次,按照Firebase文档获取推送通知。

Third, when you are ready, store the Firebase Push Notification Tokens wherever necessary in your Parse-Server. 第三,准备就绪后,将Firebase推送通知令牌存储在Parse服务器中的必要位置。

This will save you money in the long run because FCM is free. 从长远来看,这将为您省钱,因为FCM是免费的。 And if you interfere push FC Mwith Parse-Server, it will use up your dynos. 而且,如果您干扰推送FC Mwith Parse-Server,它将耗尽您的测功机。

i had the same problem.我有同样的问题。 i searched a lot and found it after you've done everything that said in docs here: https://www.back4app.com/docs/android/push-notifications/parse-server-push-notifications我搜索了很多,在你完成这里文档中所说的所有内容后找到了它: https://www.back4app.com/docs/android/push-notifications/parse-server-push-notifications

you need to change some codes你需要更改一些代码

configure android.manifiest.xml.配置 android.manifiest.xml。 before application tag ends在应用程序标签结束之前

<meta-data
          android:name="com.parse.SERVER_URL"
           android:value="@string/back4app_server_url" />
<meta-data
           android:name="com.parse.APPLICATION_ID"
           android:value="@string/back4app_app_id" />
<meta-data
           android:name="com.parse.CLIENT_KEY"
           android:value="@string/back4app_client_key" />

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

then in your class where you initialize parse server:然后在您的 class 中初始化解析服务器:

Parse.initialize(new Parse.Configuration.Builder(this)
    .applicationId(getString(R.string.back4app_app_id))
    .clientKey(getString(R.string.back4app_client_key))
    .server("https://parseapi.back4app.com/")
    .enableLocalDataStore() // add this
    .build());

Parse.setLogLevel(Parse.LOG_LEVEL_VERBOSE); // add this

ParseInstallation installation = 
    ParseInstallation.getCurrentInstallation();
installation.put("GCMSenderId", "PASTE_HERE_YOUR_GCMSenderId");
installation.saveInBackground();

and your done.你完成了。 in dashboard send push.在仪表板中发送推送。 you will get that你会得到的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM