广播接收器没有在OnReceive方法中获取PendingIntent数据吗?

时间:2019-12-03 10:39:33

标签: android notifications

在我的应用中特定的日期和时间,我正在安排通知。我收到该通知,但是当我轻按它时,便无法打开相应的活动。调试后,我知道在广播接收器中,意图数据不可用。

因此,intent.getExtra()没有收到来自SourceActivity的数据。如何解决这个问题?

SourceActivity:

Intent intent = new Intent(this.getApplicationContext(), NotificationReceiver.class);
        intent.putExtra("Id", this.Id);
        intent.putExtra("isBoolean", true);


        int id = (int) System.currentTimeMillis();      //unique id for each notifications

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

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

        if(alarmManager != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alertTime.getTimeInMillis(), pendingIntent);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, alertTime.getTimeInMillis(), pendingIntent);
            } else {
                alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime.getTimeInMillis(), pendingIntent);
            }
        }

NotificationReceiver.class:

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

        Id = intent.getIntExtra("Id", -1);      
        isBoolean= intent.getBooleanExtra("isBoolean", false);


        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            initDatabases(context);
            refreshNotificationList(context, false);
        } else {
            Intent service1 = new Intent(context, NotificationService.class);
            service1.putExtra("Id", Id);
            service1.putExtra("isBoolean", isBoolean);  

            if (service1 != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    context.startForegroundService(service1);
                } else {
                    context.startService(service1);
                }
            }
        }

NotificationService.class:

@Override
    public int onStartCommand(Intent intent, int flags, int startId) 
    {
        mManager = (NotificationManager) this.getApplicationContext().
                getSystemService(Context.NOTIFICATION_SERVICE);

        if(intent != null)
        {
            int Id = intent.getIntExtra("Id",-1);           
            boolean isBoolean = intent.getBooleanExtra("isBoolean ", false);


            createNotificationChannel();

            Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class);
            intent1.putExtra("isBoolean", isBoolean );  
            intent1.putExtra("Id", Id);

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

            PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
                    this.getApplicationContext(),
                    id,
                    intent1,
                    PendingIntent.FLAG_UPDATE_CURRENT);

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


                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                            .setSmallIcon(R.drawable.notificationIcon)
                            .setContentTitle("Alert Notification")
                            .setSound(soundUri)
                            .setAutoCancel(true)
                            .setContentIntent(pendingNotificationIntent);
                }
                else {
                    mBuilder = new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.notificationIcon)
                            .setContentTitle("Alert Notification")
                            .setSound(soundUri)
                            .setAutoCancel(true)
                            .setContentIntent(pendingNotificationIntent);
                }


            Notification note = mBuilder.build();
            note.defaults |= Notification.DEFAULT_VIBRATE;
            note.defaults |= Notification.DEFAULT_LIGHTS;
            note.flags |= Notification.FLAG_AUTO_CANCEL;

            mManager.notify(notificationId, note);

        }
        return Service.START_STICKY;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        // TODO Auto-generated method stub
        super.onTaskRemoved(rootIntent);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        mManager.cancel(notificationId);
    }


    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            mManager.createNotificationChannel(serviceChannel);
        }
    }

AndroidManifest.xml:

 <service
            android:name=".NotificationService"
            android:enabled="true"`enter code here`
           android:exported="false"
            android:process=":remote"/>

        <receiver
            android:name=".NotificationReceiver"
            android:enabled="true"
            android:exported="true"
            android:directBootAware="true"
            android:label="com.example.scienceReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

0 个答案:

没有答案