Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tap for more information or stop the app

i create service and custom notification any thing work fine but android said app is running on notification tap for more information or to stop the app. how can i fix this.

my custom BroadcastReceiver

public class ServiceBootCompleteReciver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    Intent service = new Intent(context, ServiceNotification.class);
    context.startService(service);

  }

DO i need Unregister this Receiver?

my custom nitification class

    public class CustomNotification extends Notification {
      public void notifiaction(String txt) {
        ThreadSms thread = new Thread();
        threadSms.start();
;
        if (ThreadSms.newMessage) {
          Intent notificationIntent = new Intent(GlobalController.currentActivity, ActivityMain.class);
          PendingIntent pendingIntent = PendingIntent.getActivity(GlobalController.context, 0, notificationIntent, 0);
          Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
          RemoteViews remoteViews = new RemoteViews(GlobalController.context.getPackageName(), R.layout.custom_toast);
          remoteViews.setTextViewText(R.id.text, txt);
          NotificationCompat.Builder builder = new NotificationCompat.Builder(GlobalController.context)
            .setSmallIcon(R.drawable.small_icon)
            .setContent(remoteViews)
            .setContentIntent(pendingIntent)
            .setSound(defaultSoundUri).setAutoCancel(false)
            .setLargeIcon(BitmapFactory.decodeResource(GlobalController.context.getResources(), R.drawable.modem))
            .setColor(Color.argb(255, 255, 152, 0))
            .setPriority(Notification.PRIORITY_MAX);
          GlobalController.notificationManager.notify(0, builder.build());

        }
      }
    }

my custom service

public class ServiceNotification extends IntentService {

  public ServiceNotification() {
    super("notify");
  }

  @Override
  public void onCreate() {
    super.onCreate();
  }

  @Override
  public int onStartCommand(final Intent intent, int flags, int startId) {

      GlobalController.timerTaskService = new TimerTask() {
        @Override
        public void run() {
          CustomNotification customNotification = new CustomNotification();
          customNotification.notifiaction(message);
          startForeground(1, customNotification);
        }
      };

      GlobalController.timerService.scheduleAtFixedRate(GlobalController.timerTaskService, 0, 5000);

    return Service.START_STICKY;
  }

  public void onTaskRemoved(Intent rootIntent) {
    Intent restartService = new Intent(getApplicationContext(),
      this.getClass());
    restartService.setPackage(getPackageName());
    PendingIntent restartServicePI = PendingIntent.getService(getApplicationContext(), 1, restartService, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 5000, restartServicePI);

  }

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          android:versionCode="1"
          android:versionName="1.0">

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="25"/>

    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <application
        android:name=".app.GlobalController"
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:supportsRtl="false"
        android:theme="@style/AppTheme">
        <activity android:name=".activity.ActivityLogin">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".activity.ActivitySplashScreen"/>
        <activity android:name=".activity.ActivityMain"/>
        <activity android:name=".activity.ActivityAdvanceSetting"/>
        <receiver
            android:name=".services.ServiceBootCompleteReciver"
            android:enabled="true"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>
        <service
            android:name=".services.ServiceNotification"
            android:enabled="true"
            android:exported="false"/>
    </application>
like image 378
user3750973 Avatar asked Jan 16 '17 17:01

user3750973


People also ask

How do I stop my settings from running notifications?

To find your notifications, from the top of your phone screen, swipe down. Touch and hold the notification, and then tap Settings . Choose your settings: To turn off all notifications, turn off All notifications.

How do I turn on notifications for access on Android?

Pick “Apps & Notifications”. Then “Advanced”. Then Special app access. Now pick “Android” and turn it on.


1 Answers

On Android 10, you MUST call setSmallIcon on your notification, otherwise the notification will be ignored. (The same is not true on Android 11, where it works regardless of whether you set the small icon.) If you fail to set the small icon, instead of your notification you will see a canned Android notification instead of your own:

enter image description here

This code with setSmallIcon works:

                new Notification.Builder(getApplicationContext(), channelId)
                        .setSmallIcon(Icon.createWithResource(this, R.drawable.myicon))
                        .setLargeIcon(Icon.createWithResource(this, R.drawable.myicon))
                        .setContentTitle(title)
                        .setContentText(text)
                        .setCategory(Notification.CATEGORY_CALL)
                        .setExtras(extras)
                        .setAutoCancel(true)
                        .addAction(android.R.drawable.ic_menu_call, "Accept", piAcceptIntent)
                        .setFullScreenIntent(pendingIntent, true);

This code without setSmallIcon causes the problem:

                new Notification.Builder(getApplicationContext(), channelId)
                        .setLargeIcon(Icon.createWithResource(this, R.drawable.myicon))
                        .setContentTitle(title)
                        .setContentText(text)
                        .setCategory(Notification.CATEGORY_CALL)
                        .setExtras(extras)
                        .setAutoCancel(true)
                        .addAction(android.R.drawable.ic_menu_call, "Accept", piAcceptIntent)
                        .setFullScreenIntent(pendingIntent, true);

In the comments above, @royas and @Flamychandayo found this solution. I am adding this as an answer to help make the solution more clear to those who come across this question.

like image 194
davidgyoung Avatar answered Oct 15 '22 11:10

davidgyoung