4

I want to create a notification from my app when a new picture is taken from the camera app. I want to achieve this when my app is not running. I am using Broadcast receiver to do this. Here is my Code...

In Android Manifest..

<receiver
    android:name=".receivers.CameraEventReceiver"
    android:label="CameraEventReceiver"
    android:enabled="true">
    <intent-filter>
        <action android:name="android.hardware.action.NEW_PICTURE" />

        <data android:mimeType="image/*" />
    </intent-filter>
</receiver>

In my receiver class

public class CameraEventReceiver extends BroadcastReceiver {

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

            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){

                NotificationChannel channel = new NotificationChannel("CHANNEL_ID", "my channel name", NotificationManager.IMPORTANCE_HIGH);
                manager.createNotificationChannel(channel);
            }
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "CHANNEL_ID")
                    .setColor(ContextCompat.getColor(context, R.color.colorPrimary))
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("Picture Taken")
                    .setContentText("here is the uri : "+intent.getData())
                    .setDefaults(NotificationCompat.DEFAULT_VIBRATE)
                    .setAutoCancel(true);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
                builder.setPriority(NotificationCompat.PRIORITY_HIGH);
            }

            manager.notify(222, builder.build());
        }
    }

It is working fine for Android 6.0 when the app is running... But it is not working for newer versions. What can I do to achieve this? I want it to support for all devices with android versions greater than 4.1 (JELLY_BEAN)

Thanks in advance

Abani
  • 233
  • 2
  • 8
  • Try using notification channels for latest versions of android, refer to this : https://stackoverflow.com/questions/49590489/how-to-show-a-notification-on-oreo/49594694#49594694 – Deep Patel Apr 01 '18 at 04:10

2 Answers2

0

But it is not working for newer versions.

Correct. Those broadcasts are no longer supported. See the Android 7.0 release notes.

What can I do to achieve this?

Take the picture yourself, whether using ACTION_IMAGE_CAPTURE, the camera APIs, or libraries (e.g., Fotoapparat, CameraKit-Android).

There is no direct replacement for that broadcast, and there was no requirement for camera apps to trigger that broadcast anyway.

You could use JobScheduler and addTriggerContentUri() to monitor the MediaStore for changes. However, I do not know how you would limit it to only pictures taken by a camera app.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • But in this https://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_PICTURE document. They said that **"In Android O this broadcast has been brought back, but only for registered receivers"** .. So it is supposed to work on Android O right? – Abani Mar 31 '18 at 17:10
  • @user9333500: By "registered receivers", I believe that they mean registered at runtime via `registerReceiver()`. That will not work when your process is not running. – CommonsWare Mar 31 '18 at 17:14
  • I tried using registerReceiver() at runtime in my Activity, but it was still not working. – Abani Mar 31 '18 at 17:17
  • @user9333500: Then perhaps the documentation is wrong. Or, perhaps the camera app on that particular device does not trigger this broadcast. Please bear in mind that there are hundreds of camera apps. There is no requirement for any of them to trigger this broadcast. – CommonsWare Mar 31 '18 at 17:19
  • Yeah. That's right... I tried using the default Camera app. Anyway, thanks for your response. – Abani Mar 31 '18 at 18:13
  • @user9333500: "I tried using the default Camera app" -- there are dozens of default camera apps, across the ~10,000 Android device models. – CommonsWare Mar 31 '18 at 18:17
  • Ok... That means some might not trigger the broadcast.. So is there any alternative solution for this... Can I do this using firebaseJobDispatcher since I need older version to support also? – Abani Mar 31 '18 at 18:37
  • @user9333500: "So is there any alternative solution for this" -- I outlined the options that I know of in the answer. – CommonsWare Mar 31 '18 at 18:55
  • BroadReceiver not firing though registered dynamically in android pie and android Q – Saiful Islam Sajib Oct 31 '19 at 05:15
  • is the MediaStore method how GPhotos detects whenever another app has taken a picture so it can upload it to the cloud? – Michael Sep 24 '22 at 16:04
  • also, is it possible to work around this loss of functionality by specifically targeting a version of android lower than 7.0? – Michael Sep 24 '22 at 16:05
  • 1
    @Michael: "is the MediaStore method how GPhotos detects whenever another app has taken a picture so it can upload it to the cloud?" -- I have no idea, but it's certainly a possibility. There is an option with `JobScheduler` to monitor a `ContentProvider` for changes. "is it possible to work around this loss of functionality by specifically targeting a version of android lower than 7.0?" -- no. – CommonsWare Sep 24 '22 at 17:19
0

I have implemented when new SMS arrived and i was facing the same issue. I have put below lines in my manifest file.

<intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.INPUT_METHOD_CHANGED" />
</intent-filter> 
Komal
  • 328
  • 3
  • 15