Search code examples
androidbroadcastreceiverextra

Android - Intent in BroadcastReceiver lost String


I want to use BroadcastReceiver to send notification with specific String. Here is my function in MainActivity:`

private void createAlarm (int i) throws ParseException {
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlarmReceiver.class);
        intent.putExtra("name", names.get(i));

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
                (int) timeInMillis / 1000),
                intent,
                PendingIntent.FLAG_ONE_SHOT
        );

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);
        }
    }

BroadcastReceiver code:

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String name = intent.getExtras().getString("name", "deafultValue");

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            Intent notificationIntent = new Intent(context, MainActivity.class);
            TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
            taskStackBuilder.addNextIntentWithParentStack(notificationIntent);
            PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(50, PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "EventStartChannel")
                    .setSmallIcon(R.drawable.ic_stat_name)
                    .setContentTitle(name)
                    .setContentText(name)
                    .setColor(Color.argb(255, 255, 0, 0))
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
            notificationManager.notify((int) System.currentTimeMillis() / 1000, builder.build());
        }
    }
}

And manifest code:

        <receiver android:name=".OtherClasses.AlarmReceiver"
            android:enabled="true"
            android:exported="false" >

            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.SEND" />
            </intent-filter>

        </receiver>

But sometimes, when alarm is set on one day later or more, extras in onReceive are empty and name get deafult value. I've tried passing string with bundle in extras and then was the same problem.


Solution

  • To send

    Change this:

    intent.putExtra("name", names.get(i));
    

    To:

    intent.putExtra(android.content.Intent.EXTRA_TEXT, names.get(i));
    

    To get

    Change this:

    String name = intent.getExtras().getString("name", "deafultValue");
    

    To:

    String name = intent.getStringExtra(android.content.Intent.EXTRA_TEXT);