0

I have two devices. one is running API 26, and the other is API 21. I am using Googles firebase for the notifications. The API 26 device does not receive push notifications unless I open the app first. Then it receives everything fine. I do not have to open the app first to receive the push notifications on the API 27 device. Any thoughts on how to always receive the pushes even without opening the app? I want the notifications to reach the user by just turning on their phone.

private void sendNotification(String title, String messageBody, String clickAction) {

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String id = "";
    if (Build.VERSION.SDK_INT >= 26) {
        id = "my_channel_01";
        // The user-visible name of the channel.
        CharSequence name = "myChannel";
        // The user-visible description of the channel.
        String description = "";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(id, name,importance);
        // Configure the notification channel.
        mChannel.setDescription(description);
        mChannel.enableLights(true);
        // Sets the notification light color for notifications posted to this
        // channel, if the device supports this feature.
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        notificationManager.createNotificationChannel(mChannel);
    }


    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, id)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(title)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);


    notificationManager.notify(1 /* ID of notification */, notificationBuilder.build());


}
Zoe
  • 27,060
  • 21
  • 118
  • 148
RobOhRob
  • 585
  • 7
  • 17

1 Answers1

4

Add BootBroadcastReceiver in Manifest file

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

Create Following Java file. call the FirebaseMessagingReceiveService inside it.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class OnBootBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent("com.demo.FirebaseMessagingReceiveService");
        i.setClass(context, FirebaseMessagingReceiveService.class);
        context.startService(i);
    }
}
jessica
  • 1,700
  • 1
  • 11
  • 17