Я пишу этот код для отправки уведомления, но не работаю в android 10 !! Я не могу решить эту проблему. Как решить эту проблему?
Этот код работает в android ниже 10. Это работает в android p ie (API 28), но не работает в android 10
Мой класс обслуживания для отправки уведомления ниже кода
public class NotificationService extends Service {
Timer timer;
TimerTask timerTask;
String TAG = "Timers";
int Your_X_SECS = 20;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
startTimer();
return START_STICKY;
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
// stoptimertask();
super.onDestroy();
// startTimer();
}
//we are going to use a handler to be able to run in our TimerTask
final Handler handler = new Handler();
public void startTimer() {
timer = new Timer();
initializeTimerTask();
timer.schedule(timerTask, 5000,1000); //
}
public void stoptimertask() {
//stop the timer, if it's not already null
if (timer != null) {
timer.cancel();
timer = null;
}
}
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run () {
handler .post( new Runnable() {
public void run () {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
createNotification();
}
}) ;
}
} ;
}
private void createNotification () {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE ) ;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext() ,
"1" ) ;
mBuilder.setContentTitle( "My Notification" ) ;
mBuilder.setContentText( "Notification Listener Service Example" ) ;
mBuilder.setTicker( "Notification Listener Service Example" ) ;
mBuilder.setSmallIcon(R.drawable. ic_launcher_foreground ) ;
mBuilder.setAutoCancel( true ) ;
if (android.os.Build.VERSION. SDK_INT >= android.os.Build.VERSION_CODES. O ) {
int importance = NotificationManager. IMPORTANCE_HIGH ;
NotificationChannel notificationChannel = new NotificationChannel( "1" ,
"NOTIFICATION_CHANNEL_NAME" , importance) ;
mBuilder.setChannelId( "1" ) ;
assert mNotificationManager != null;
mNotificationManager.createNotificationChannel(notificationChannel) ;
}
assert mNotificationManager != null;
mNotificationManager.notify(( int ) System. currentTimeMillis () , mBuilder.build()) ;
}
}
Класс BoradcastReceiver ниже кода
public class Restarter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Broadcast Listened", "Service tried to stop");
Toast.makeText(context, "Service restarted", Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, NotificationService.class));
} else {
context.startService(new Intent(context, NotificationService.class));
}
}
androidmonifest. xml здесь
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.project.jetpack.myapplication">
<application
android:name=".BaseApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".ui.drugplan.activity.EditDrugPlanActivity"
android:configChanges="uiMode"/>
<activity android:name=".ui.drugplan.activity.DrugPlanDetailActivity"
android:configChanges="uiMode"/>
<activity android:name=".ui.drug.MoreOptionsActivity"
android:configChanges="uiMode"/>
<activity android:name=".ui.category.activity.AddCategoryActivity"
android:configChanges="uiMode"/>
<activity
android:name=".ui.drugplan.activity.AddDrugPlanActivity"
android:configChanges="uiMode" />
<activity android:name=".ui.drugplan.activity.DrugPlanListActivity"
android:configChanges="uiMode" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".notification.NotificationService"
android:label="@string/app_name">
<intent-filter>
<action android:name="your.app.domain.NotificationService" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
<receiver
android:name=".notification.Restarter"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="restartservice" />
</intent-filter>
</receiver>
</application>
</manifest>