JobService skips some sms in android oreo - java

I am creating a job service.
This service will be triggered based on change in sms(sent), works fine but failed to catch some messages.
ComponentName component = new ComponentName(context, jobTest.class);
Uri uri = Uri.parse("content://sms");
JobInfo.TriggerContentUri triggerContentUri = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
triggerContentUri = new JobInfo.TriggerContentUri(uri,
JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS);
}
JobInfo.Builder builder = new
JobInfo.Builder(1, component).addTriggerContentUri(triggerContentUri);
JobScheduler jobScheduler = (JobScheduler)
context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(builder.build());
I am catching only sent messages through this job,When i sent multiple messages to a number, it catches only latest message.
I don't want to use foreground service.

to trigger immediately,You need to add setTriggerContentMaxDelay
JobInfo.Builder builder = new
JobInfo.Builder(1, component).addTriggerContentUri(triggerContentUri).setTriggerContentMaxDelay(0);

Related

How to create notification channel for PermissionEverywhere.getpermission

I am working on one task where I need to read the data from calendar, as per android documentation, I added
<uses-permission android:name="android.permission.READ_CALENDAR" />
but now when I am using a method
ActivityCompat.checkSelfPermission(this,
Manifest.permission.READ_CALENDAR)
it is always returning -1, and permission is denial always, After investigation, I found that I have to request permission at run time, and there are multiple permission required to be granted in my framework,
I found that PermissionEverywhere.getpermission, can solve this issue,
but I am getting error "Developer warning for package Failed to post notification on channel "null" see log for more details
I tried adding the notification channel using below method
private void createNotificationChannelIfNeeded(int id, String title, String message) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = title;
String description = message;
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(title, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager mNotifyMgr =
(NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.createNotificationChannel(channel);
}
but still getting the same error. please let me know, how to create channel for Permission everywhere class

Sending notification to a particular user or a particular channel in android using PHP

i have been working on an APP where people registers and then subscribes for some kind of specific notifications i have created the APP already (pretty much done)
i am new to the APP develelopment and have less knowledge about JAVA so if i am wrong anytime please do let me know thankx
What i have now
user can login or signup etc and when signup i create a notification chanel with his/her phone number
NOTIFICATION CODE
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = channel_name;
String description = channel_description;
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("124t", name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
now i have no idea what to do after this how do i check the notification channels created and send test notification using some kind of API etc.
any reference or answer is appreciated.
Thankx

Unable to instantiate receiver com.google.firebase.iid.FirebaseInstanceIdReceiver: java.lang.ClassNotFoundException:

I have checked many answers, but they didn't work. I just want to create simple push notification app. First I created for single device when I made an apk and tried to send notification on multiple devices it starts crashing.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
String TAG ="MALIK";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
sendNotification(remoteMessage.getNotification().getBody());
// TODO(developer): Handle FCM messages here.
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
if (/* Check if data needs to be processed by long-running job */ true) {
// For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
} else {
// Handle message within 10 seconds
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody());
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
#Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
}
private void sendNotification(String messageBody) {
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, channelId)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle(getString(R.string.fcm_message))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
The error can be summarized to ClassNotFoundException.
It means the code called a class that is not in the class path.
The issue can be because:
The library Firebase-iid needs to be added.
// use compile instead of implementation if it's unknown
implementation "com.google.firebase:firebase-iid:+" // 18.0.0 or 20.0.0
The library firebase-messaging is added (and adds this class too), but it's very old or very new, so it's not compatible with your code.
Make sure you add firebase messaging to the project:
// use compile instead of implementation if it's unknown
implementation "com.google.firebase:firebase-messaging:+" // 18.0.0 or 20.0.0
If your code is old (copied from an ancient post blog), add a lower firebase-messaging library version. Else if your code is new and your firebase-messaging is old, try upgrading the library version.
More info on firebase release notes.
In my case this has issue fixed by adding this dependency:
implementation "androidx.legacy:legacy-support-core-utils:1.0.0"
I found this solution accidentally. If it doesn't work give me feedback to edit/delete my post.

job scheduler not working in xiaomi android

The Job Schedular set as follows
ComponentName mServiceComponent = new ComponentName(context, TestJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(jobId, mServiceComponent);
builder.setPeriodic(3 * 60 * 1000);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_NONE);
builder.setRequiresDeviceIdle(false);
builder.setRequiresCharging(false);
builder.setPersisted(true);
JobScheduler jobScheduler = (JobScheduler) ChaseForceApplication.getAppContext().getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(builder.build());
The TestJobService class is like this:
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class TestJobService extends JobService {
#Override
public boolean onStartJob(JobParameters params) {
Utility.writeToTheFile(ChaseForceApplication.getAppContext(), "\n\n Job Scheduler StartJob with jobid="+params.getJobId()+" set at " + new Date().toString());
sendBroadcast(new Intent(this, OnSingleAlarmReceiver.class));
return false;
}
#Override
public boolean onStopJob(JobParameters params) {
Log.i(ChaseForceApplication.TAG, "on stop job: " + params.getJobId());
Utility.writeToTheFile(this, "on stop job: " + new Date().toString());
return false;
}
}
It's working on most devices, even in other xiaomi phones but in Xiaomi Redmi 3S it is not working.
Is any setting required for Job Schedular to make it work on that device?
It seems that Xiaomi MIUI operative system don't allow JobScheduler to run https://web.archive.org/web/20171001070316/http://c.mi.com/thread-8779-1-1.html
From your app settings/info page, try to enable auto start and then retry with job scheduler. It will work. You need to enable autostart for your app.

Sending a Notification to Pebble

I'm trying to send a notification to my pebble watch. I'm using this code, which is basically the example from the website:
public void sendPebble(String title, String body) {
final Intent i = new Intent("com.getpebble.action.SEND_NOTIFICATION");
final Map<String, String> data = new HashMap<String, String>();
data.put("title", title);
data.put("body", body);
final JSONObject jsonData = new JSONObject(data);
final String notificationData = new JSONArray().put(jsonData).toString();
i.putExtra("messageType", "PEBBLE_ALERT");
i.putExtra("sender", "Test");
i.putExtra("notificationData", notificationData);
Log.d("Test", "Sending to Pebble: " + notificationData);
sendBroadcast(i);
}
I'm getting the message in LogCat, but no notification on the watch. The procedure seems simple enough, is there something too obvious I missed? Or is the documentation just incomplete?
Edit: The obvious questions:
Yes, the watch is connected
Yes, I have third party notifications enabled
Okay, this was my problem:
By default the Pebble app only sends notifications to the watch while the screen of the phone is off. For development I always have the screen active while the phone is connected via USB. So, the solution was: Enable the "Always send Notifications" option in the Pebble app.
Maybe this spares someone else a headache.

Categories