Pu sh уведомление в разборе Server - PullRequest
0 голосов
/ 08 января 2020

Я внедряю уведомление Firebase pu sh через Parse Server.

Когда я отправляю уведомление через Dashboard, оно отображается как отправленное в прошлом.

Я не могу получить / не получить получение pu sh уведомления

  1. AndroidManifeast.xml

    <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.DELETE" />
            <action android:name="com.parse.push.intent.OPEN" />
        </intent-filter>
    </receiver>


    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_trot_icon" />

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="@string/default_notification_channel_id" />

    <meta-data
        android:name="firebase_messaging_auto_init_enabled"
        android:value="false" />
    <meta-data
        android:name="firebase_analytics_collection_enabled"
        android:value="false" />

    <meta-data
        android:name="com.parse.SERVER_URL"
        android:value="@string/parse_server_url" />

    <meta-data
        android:name="com.parse.APPLICATION_ID"
        android:value="@string/parse_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="94570192751" />

build.gradle

implementation "com.github.parse-community.Parse-SDK-Android:parse:1.22.1" implementation "com.github.parse-community.Parse-SDK-Android:fcm:1.22.1"

Я добавляю данные в MainActivity. java

ArrayList<String> channels = new ArrayList<>(); channels.add(ParseConstants.NOTIFICATION_CHANNEL_PLACES); channels.add(ParseConstants.NOTIFICATION_CHANNEL_FRIENDS); channels.add(ParseConstants.NOTIFICATION_CHANNEL_SYSTEM);<br> ParseInstallation installation = ParseInstallation.getCurrentInstallation(); installation.put(ParseConstants.GCM_SENDER_ID, getString(R.string.gcm_sender_id)); installation.put(ParseConstants.CHANNEL, channels); installation.put(ParseConstants.USERNAME,ParseUser.getCurrentUser().getUsername()); installation.saveInBackground();

1 Ответ

0 голосов
/ 09 января 2020

1. Я удалил ниже зависимости

реализация 'com.google.firebase: firebase-analytics: 17.0.1'

реализация 'com.google.firebase: firebase-messaging: 20.1. 0 '

реализация' com.google.firebase: firebase-core: 17.2.1 '

2. Добавлена ​​

реализация 'com.google. android .gms: play-services-gcm: 17.0.0'

3. В Androidmanifest.xml добавлено

<permission
        android:name="packagename.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

 <uses-permission android:name="packagename.permission.C2D_MESSAGE" />

В application тег

    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
            <!-- for Gingerbread GSF backward compat -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="packagename" />
        </intent-filter>
    </receiver>

    <service
        android:name=".utils.PushNotificationReceiver" android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>

4. Создан новый класс PushNotificationReceiver extends GcmListenerService

переопределить onMessageReceived

@Override
public void onMessageReceived(String from, Bundle data) {
    try {
            if (data.containsKey("data")) {
                JSONObject jsonObject = new JSONObject(data.getString("data"));
                sendNotification(jsonObject.getString("title"), jsonObject.getString("message"),
                        jsonObject.getString("channel"));
            }
        } catch (Exception ex) {

        }
}


private void sendNotification(String title, String message, String channel) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel notificationChannel = new NotificationChannel(channel,
                        channel, NotificationManager.IMPORTANCE_HIGH);
                // Configure the notification channel.
                notificationChannel.setDescription("Channel description");
                notificationChannel.enableLights(true);
                notificationChannel.setLightColor(Color.RED);
                notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
                notificationChannel.enableVibration(true);
                if (notificationManager != null) {
                    notificationManager.createNotificationChannel(notificationChannel);
                }
            }

            Intent intent = new Intent(this, Splash.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

            NotificationCompat.Builder notificationBuilder =
                    new NotificationCompat.Builder(this, channel);
            notificationBuilder.setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.trot_icon_with_space)
                    .setTicker("Hearty365")
                    //     .setPriority(Notification.PRIORITY_MAX)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setContentIntent(pendingIntent)
                    .setContentInfo("Info");

            if (notificationManager != null) {
                int pushNotificatonId = new Random().nextInt(10000);
                notificationManager.notify(/*notification id*/pushNotificatonId, notificationBuilder.build());
            }
        }
...