使用FCM的Android 8.1 Oreo不提供推送通知

时间:2018-07-14 19:02:11

标签: android firebase push-notification android-8.1-oreo

我正在处理FCM推送通知。直到Android O更低版本都可以正常工作。我已经查看了Android Docs,并在推送通知中也做了Channels,但是问题是我的CustomPushReceiver中没有推送信息。对于较低的版本,它工作得很好。我的自定义推送接收器以及清单代码如下:

public class CustomPushReceiver extends ParsePushBroadcastReceiver {

    int importance = NotificationManager.IMPORTANCE_HIGH;
    private SharedPreferences prefs ;
    Boolean languageBoolean = true;

    public CustomPushReceiver() {
        super();
        Log.e("CustomPushReceiver","Come to this portion");
    }


    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        Log.e("onReceive","Come to this portion");
    }

    @Override
    public void onPushReceive(Context context, Intent intent) {

        Log.e("onPushReceive","Come to this portion");
        if (intent == null)
            return;
        try {
            String action = intent.getAction();
            JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
            if (action.equalsIgnoreCase("com.parse.push.intent.RECEIVE")) {
                if (json.has("alert"))
                {
                    String message = json.getString("alert");

                    localPushNotifications26GreaterAPI(context, message, json);
                }
            }
        } catch (JSONException e) {
        }
    }

    @Override
    protected void onPushDismiss(Context context, Intent intent) {
        super.onPushDismiss(context, intent);
        Log.e("onPushDismiss","Come to this portion");
    }

    @Override
    protected void onPushOpen(Context context, Intent intent) {
        super.onPushOpen(context, intent);
        Log.e("onPushOpen","Come to this portion");
    }


    public void localPushNotifications26GreaterAPI(Context context, String message, JSONObject jsonObject){

        Log.e("localPushNoti26API","Come to this portion");

        try {
            if (ParseUser.getCurrentUser() != null) {
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

                    Intent intent = new Intent(context, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

                    // The id of the channel.
                    String id = "my_channel_01";
                    CharSequence name = "com.dcas.responder";
                    String description = "Description";

                    NotificationChannel mChannel = new NotificationChannel(id, name, importance);
                    mChannel.setDescription(description);
                    mChannel.enableLights(true);
                    mChannel.setLightColor(Color.RED);
                    mChannel.enableVibration(true);
                    mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                    mNotificationManager.createNotificationChannel(mChannel);
                    mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                    int notifyID = 1;
                    String CHANNEL_ID = "my_channel_01";

                String contentTitle = "", contentText ="", messageText ="";
                    if (languageBoolean == true) {
                        contentTitle =  "Dubai Corporation Ambulance Services";
                        contentText  =  "Emergency Request";
                        messageText  =  message;
                    }else{
                        contentTitle =  context.getString(R.string.dubai_corporation_for_ambulance_services);
                        contentText  =  context.getString(R.string.emergency_request);
                        messageText  =  context.getString(R.string.press_here_to_get_medical_help_in_minutes);
                    }

                    Notification notification = new Notification.Builder(context, CHANNEL_ID)
                            .setContentTitle(contentTitle)
                            .setContentText(contentText)
                            .setStyle(new Notification.BigTextStyle().bigText(messageText))
                            .setContentIntent(contentIntent)
                            .setSmallIcon(R.drawable.push_icon)
                            .setChannelId(CHANNEL_ID)
                            .build();
                    notification.flags |= Notification.FLAG_AUTO_CANCEL;

                    mNotificationManager.notify(notifyID, notification);
                } else {
                    localPushNotifications26LowerAPI(context , message);
                }

            }
        }catch(Exception exp){
            exp.getMessage();
        }

    }


    public void localPushNotifications26LowerAPI(Context context, String message){

        Log.e("localPushNotiLowAPI","Come to this portion");

        if (ParseUser.getCurrentUser() != null) {
            Intent intent = new Intent(context, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);


            NotificationCompat.Builder b = new NotificationCompat.Builder(context);

            String contentTitle = "", contentText ="", messageText ="";
            if (languageBoolean == true) {
                contentTitle =  "Dubai Corporation Ambulance Services";
                contentText  =  "Emergency Request";
                messageText  =  message;
            }else{
                contentTitle =  context.getString(R.string.dubai_corporation_for_ambulance_services);
                contentText  =  context.getString(R.string.emergency_request);
                messageText  =  context.getString(R.string.press_here_to_get_medical_help_in_minutes);
            }

            b.setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.push_icon)
                    .setTicker(contentText)
                    .setContentTitle(contentTitle)
                    .setContentText(messageText)
                    .setContentIntent(contentIntent)
                    .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
                    .setContentIntent(contentIntent)
                    .setContentInfo("Info");

            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

            notificationManager.notify(1, b.build());


        }
    }
}

我的清单文件中,我声明了我的FCM设置和CustomPushReceiver:

<receiver
            android:name="receivers.CustomPushReceiver"
            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>
        <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.dcas.responder" />
            </intent-filter>
        </receiver>

<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

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

我要初始化和订阅解析推送通道的Application类:

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

ParsePush.subscribeInBackground("dcas" + ParseUser.getCurrentUser().getObjectId(), new SaveCallback() {
                                @Override
                                public void done(ParseException e) {
                                    if(e==null){
                                        Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
                                        startActivity(intent);
                                        finish();
                                    }
                                }
                            });

这在Android 27较低版本上可以很好地运行,而且仅在某些时候它也可以在Android 27版本上运行,这很奇怪,没有任何理由。

0 个答案:

没有答案