简体   繁体   中英

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.

Here's my index.js configurations:

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

here's my 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:

  @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.

PS: the minimum sdk version i'm using is 19

I had the same problem.

First, stop trying to use Parse-Server directly for Push Notifications. You shouldn't be messing the with the index.js file. Just stick to the firebase docs for FCM.

Second, Follow the Firebase docs for push notifications.

Third, when you are ready, store the Firebase Push Notification Tokens wherever necessary in your Parse-Server.

This will save you money in the long run because FCM is free. And if you interfere push FC Mwith Parse-Server, it will use up your dynos.

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

you need to change some codes

configure 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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