19

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>
user3750973
  • 193
  • 1
  • 6

1 Answers1

12

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.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • I was banging my head against the wall until I found this. I'm trying to create the `Notification` manually (without `Notification.Builder`) and so I found that I _must_ set the `icon` field otherwise the same thing occurs – Neil C. Obremski Jan 07 '21 at 01:11