Oreo 8.0中的推送通知问题 [英] Push notification issue in Oreo 8.0

查看:92
本文介绍了Oreo 8.0中的推送通知问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将应用程序更新为8.1后,该通知未显示,我对其进行了修复.现在,待处理的意图无法按预期工作.

After updating the app to 8.1 the notification was not shown and I fixed it. Now, pending intent is not working as expected.

收到通知后,如果它处于后台,并且无法关闭,则无法启动.

After receiving the notification, I am not able to navigate to the app if it is in background and if it is closed it is not launching.

private void sendNotify(String messageBody) {
    Intent intent = new Intent();
    intent.setAction(Constants.NOTIFY);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    int uniqueId = (int) System.currentTimeMillis();

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Creates the PendingIntent
        PendingIntent notifyPendingIntent =
                PendingIntent.getActivity(
                        this,
                        0,
                        intent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        String channelID = "com.myapp.ind.push.ServiceListener";// The id of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(channelID, "MyApp", importance);
        // Create a notification and set the notification channel.
        Notification notification = getNotificationBuilder(messageBody, notifyPendingIntent, defaultSoundUri)
                .setChannelId(channelID)
                .build();

        if (notificationManager != null) {
            notificationManager.createNotificationChannel(mChannel);
            notificationManager.notify(uniqueId, notification);
        }
    } else if (notificationManager != null) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
            uniqueId /* Request code */,
            intent,
            PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder = getNotificationBuilder(messageBody, pendingIntent, defaultSoundUri);
        notificationManager.notify(uniqueId /* ID of notification */,
                notificationBuilder.build());
    }
}

private NotificationCompat.Builder getNotificationBuilder(String messageBody, PendingIntent pendingIntent, Uri defaultSoundUri) {
    return new NotificationCompat.Builder(this)
            .setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle(getString(R.string.notification_title))
            .setContentText(messageBody)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
}

单击通知,它可以正常工作到6.0.更新至8.0后,它无法在Google Pixel设备中使用.它不是在打开应用程序或将应用程序置于前台.

On click of notification it was working fine till 6.0. After updating to 8.0 it is not working in Google Pixel devices. It is not opening the app or bringing the app to foreground.

推荐答案

创建如下所示的Receiver类.并注册清单文件.

Create a Receiver class like below. And register that is manifest file.

public class Receiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() != null
            && intent.getAction().equals("My Call")) {
            Intent startIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
            context.startActivity(startIntent);
        }
    }
}

收到通知后,Notification类中的以下代码.

The below code from the Notification class after receiving the notification.

private void sendNotification() {
    Intent intent = new Intent(this, Receiver.class);
    intent.setAction("My Call");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    int uniqueId = (int) System.currentTimeMillis();
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
            uniqueId /* Request code */,
            intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationManager != null) {
        String channelID = "Your Channel ID";// The id of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(channelID, "My_Name", importance);
        // Create a notification and set the notification channel.
        Notification notification = getNotificationBuilder(messageBody, pendingIntent, defaultSoundUri)
                .setChannelId(channelID)
                .build();

        notificationManager.createNotificationChannel(mChannel);
        notificationManager.notify(uniqueId, notification);
    } else if (notificationManager != null) {
        NotificationCompat.Builder notificationBuilder = getNotificationBuilder();
        notificationManager.notify(uniqueId /* ID of notification */,
                notificationBuilder.build());
    }
}

private NotificationCompat.Builder getNotificationBuilder() {
    return new NotificationCompat.Builder(this)
            .setSmallIcon(image)
            .setContentTitle(title)
            .setContentText(message)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
}

现在我正在所有设备上接收通知.以前我也收到过,但是由于Oreo设备中的Channel实施而没有显示在状态栏中.现在工作正常.

Now I am receiving the notification in all the device. Previously also I received but not displayed in status bar because of Channel implementation in Oreo devices. Now working perfectly.

这篇关于Oreo 8.0中的推送通知问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆