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
Related
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
I am trying to build a parental control app. So now i want to disable or lock app (like Whatsapp, Facebook, etc). I have tried using PackageManager.setComponentEnabledSetting(). But it is throwing java.lang.SercurityException.
So how can I make a parental control app such that I can disable any app I want without root.
my code is
pm.setComponentEnabledSetting(new ComponentName(temp.activityInfo.packageName,
temp.activityInfo.name+".class"),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
my error was this
java.lang.SecurityException: Permission Denial: attempt to change component state from pid=11537, uid=10067, package uid=10029
You must add below permissions to manifest.
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
but , these permissions are for System apps and you can not use. :(
You can not write a app to lock or close another app.this is a policy in Google.
for lock a app you must check running apps repeatedly, if specific app is open,then show a activity over that.
while(!Thread.currentThread().isInterrupted())
{
String topActivity = getFrontApp();
if(topActivity.isEmpty())
{
threadSleep(500);
continue;
}
if(topActivity.equals("lockApp"))
{
showLockActivity();
}
threadSleep(500);
}
// for Api21+ need permission
public static String getFrontApp()
{
if (Build.VERSION.SDK_INT >= 21)
{
UsageStatsManager usageManager = SystemMaster.getUsageStatsManager();
long now = System.currentTimeMillis();
List<UsageStats> localList = usageManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, now - 900_000L, now);
String str = "";
if (localList != null)
{
SortedMap<Long,UsageStats> mySortedMap = new TreeMap<>();
for(UsageStats usageStats : localList)
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
if(!mySortedMap.isEmpty())
str = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
}
return str;
}
else
{
ActivityManager am = (ActivityManager) getApplication().getSystemService(Context.ACTIVITY_SERVICE);
return am.getRunningTasks(1).get(0).topActivity.getPackageName();
}
above code is very simple , for real app must write more.
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.
I am building an Android Application that needs the Notification Accessibility permission.
My thought is to take the user to the Notification Accessibility page when the app is first started, How can I do this via intent?
To open NotificationAccessSettingsActivity via intent you could do something like this:
startActivity(new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS));
If you want to check the permission you could use:
private boolean isNotificationServiceRunning() {
ContentResolver contentResolver = getContentResolver();
String enabledNotificationListeners =
Settings.Secure.getString(contentResolver, "enabled_notification_listeners");
String packageName = getPackageName();
return enabledNotificationListeners != null && enabledNotificationListeners.contains(packageName);
}
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);