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