【问题标题】:PendingIntent.getBroadcast not working in OreoPendingIntent.getBroadcast 在奥利奥中不起作用
【发布时间】:2019-02-07 12:54:36
【问题描述】:

我很久以前就有一个应用在playstore上,最近通知没有向用户开放

这是我的代码

private void showNotification () {

        PhoneUtils.clearAllNotifications(getApplicationContext());

        NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        String channelId = “app”;
        int notificationId = 100;

        createNotificationChannel(channelId , notificationManager);

        Notification notification = new NotificationCompat.Builder(getApplicationContext(), channelId)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(getApplicationContext().getResources().getString(R.string.app_name))
                .setContentText(mAlert)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setAutoCancel(true)
                .setContentIntent(getOpenNotificationIntent())
                .setDefaults(Notification.DEFAULT_ALL)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .build();

        notificationManager.notify(notificationId, notification);

}

private PendingIntent getOpenNotificationIntent () {

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

        Intent intent = new Intent(“com.app.OPEN”);
        intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_TYPE, mType);
        intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_ID, mId);
        intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_DIALOG_ID, mDialogId);
        intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_MESSAGE_ID, mMessageId);
        Notification notification = null;

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestID,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

        return pendingIntent;
}

<receiver
    android:name=".fcm.OpenNotificationReceiver"
    android:exported="false">
    <intent-filter>
        <action android:name="com.app.OPEN" />
    </intent-filter>
</receiver>

【问题讨论】:

    标签: android push-notification android-pendingintent


    【解决方案1】:

    从 Android 8 (Oreo) 开始,您不能再在清单中为 隐式 Intent 注册 BroadcastReceiver。这就是你正在做的:

    <receiver
        android:name=".fcm.OpenNotificationReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="com.app.OPEN" />
        </intent-filter>
    </receiver>
    

    您应该使用显式Intent来代替这个,如下所示:

    将清单条目更改为:

    <receiver
        android:name=".fcm.OpenNotificationReceiver">
    </receiver>
    

    并将用于为Notification 创建PendingIntent 的代码更改为:

        Intent intent = new Intent(this, OpenNotificationReceiver.class);
        intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_TYPE, mType);
        intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_ID, mId);
        intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_DIALOG_ID, mDialogId);
        intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_MESSAGE_ID, mMessageId);
        Notification notification = null;
    
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestID,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    有关更多信息,请参阅https://developer.android.com/about/versions/oreo/background 并搜索“广播限制”

    【讨论】:

      【解决方案2】:

      修改getOpenNotificationIntent方法。

      private PendingIntent getOpenNotificationIntent () {
      
              int requestID = (int) System.currentTimeMillis();
      
              Intent intent = new Intent(“com.app.OPEN”);
      
              //add this line
              intent.setPackage(getApplicationContext().getPackageName());
      
              intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_TYPE, mType);
              intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_ID, mId);
              intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_DIALOG_ID, mDialogId);
              intent.putExtra(ForSaleConstants.ACTIVITY_NOTIFICATION_MESSAGE_ID, mMessageId);
              Notification notification = null;
      
              PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestID,
                      intent, PendingIntent.FLAG_UPDATE_CURRENT);
      
              return pendingIntent;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-01
        相关资源
        最近更新 更多