Skip to content

Commit

Permalink
updated to api 33 added permission for notification.
Browse files Browse the repository at this point in the history
  • Loading branch information
JimSeker committed Oct 24, 2022
1 parent 59afc1a commit e450d1a
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 28 deletions.
17 changes: 0 additions & 17 deletions BroadcastNoti/.idea/deploymentTargetDropDown.xml

This file was deleted.

3 changes: 1 addition & 2 deletions BroadcastNoti/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions BroadcastNoti/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 31
compileSdkVersion 33

defaultConfig {
applicationId "edu.cs4730.broadcastnoti"
minSdkVersion 26
targetSdkVersion 31
targetSdkVersion 33
versionCode = "1"
versionName = "1.0"
}
Expand All @@ -21,5 +21,5 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.appcompat:appcompat:1.5.1'
}
2 changes: 1 addition & 1 deletion BroadcastNoti/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http:https://schemas.android.com/apk/res/android"
package="edu.cs4730.broadcastnoti">

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package edu.cs4730.broadcastnoti;

import java.util.Calendar;
import java.util.Map;

import android.Manifest;
import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.util.Log;
import android.view.View;
Expand All @@ -22,6 +30,8 @@ public class MainActivity extends AppCompatActivity {
public static final String ACTION = "edu.cs4730.bcr.noti";
public static String id = "test_channel_01";
static String TAG = "MainActivity";
ActivityResultLauncher<String[]> rpl;
private final String[] REQUIRED_PERMISSIONS = new String[]{Manifest.permission.POST_NOTIFICATIONS};
NotificationManager nm;
TextView logger;

Expand All @@ -32,6 +42,24 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// for notifications permission now required in api 33
//this allows us to check with multiple permissions, but in this case (currently) only need 1.
rpl = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(),
new ActivityResultCallback<Map<String, Boolean>>() {
@Override
public void onActivityResult(Map<String, Boolean> isGranted) {
boolean granted = true;
for (Map.Entry<String, Boolean> x : isGranted.entrySet()) {
logthis(x.getKey() + " is " + x.getValue());
if (!x.getValue()) granted = false;
}
if (granted)
logthis("Permissions granted for api 33+");
}
}
);


//check see if there is data in the bundle, ie launched from a notification!
String info = "Nothing";
Bundle extras = getIntent().getExtras();
Expand All @@ -42,10 +70,9 @@ protected void onCreate(Bundle savedInstanceState) {
} //something wrong here.
}

Log.v("MainActivity", "info:" + info);

logger = (TextView) findViewById(R.id.textView1);
logger.setText(info);
logthis(info);

//setup button to send an intent for static registered receiver.
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -55,6 +82,12 @@ public void onClick(View view) {
});

createchannel();
//for the new api 33+ notifications permissions.
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (!allPermissionsGranted()) {
rpl.launch(REQUIRED_PERMISSIONS);
}
}
}

public void setalarm() {
Expand Down Expand Up @@ -107,4 +140,18 @@ private void createchannel() {

}

//ask for permissions when we start.
private boolean allPermissionsGranted() {
for (String permission : REQUIRED_PERMISSIONS) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}

public void logthis(String msg) {
logger.append(msg + "\n");
Log.d(TAG, msg);
}
}
2 changes: 1 addition & 1 deletion BroadcastNoti/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.3'
classpath 'com.android.tools.build:gradle:7.3.1'
}
}

Expand Down
2 changes: 1 addition & 1 deletion BroadcastNoti/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\:https://services.gradle.org/distributions/gradle-7.2-all.zip
distributionUrl=https\:https://services.gradle.org/distributions/gradle-7.4-all.zip

0 comments on commit e450d1a

Please sign in to comment.