androidremoteviewcustom-notification

Custom Notification using Remoteview not showing after Boot


I'm showing a custom notification in my app which is working fine, but I want to start this notification after device boot up, but at this point it's generating error

Error Logcat:

01-17 20:29:40.281  1652  1785 W PackageManager: Failure retrieving resources for com.ltlgen.guest_mode: Resource ID #0x0

01-17 20:30:42.721  1070  1629 D ActivityManager: startProcessLocked calleePkgName: com.ltlgen.guest_mode, hostingType: broadcast-2

01-17 20:30:42.781  1070  1629 I ActivityManager: Start proc 6558:com.ltlgen.guest_mode/u0a188 for broadcast-2 com.ltlgen.guest_mode/.BootReceiver

01-17 20:30:42.971  6558  6558 W ResourcesManager: getTopLevelResources: com.ltlgen.guest_mode for user  0

01-17 20:30:42.991  6558  6558 D InjectionManager: fillFeatureStoreMap com.ltlgen.guest_mode

01-17 20:30:43.001  6558  6558 I InjectionManager: Constructor com.ltlgen.guest_mode, Feature store :{}

01-17 20:30:43.001  6558  6558 D guest_mode.BootReceiver: android.intent.action.BOOT_COMPLETED

01-17 20:30:43.091  6558  6558 E AndroidRuntime: Process: com.ltlgen.guest_mode, PID: 6558

01-17 20:30:43.091  6558  6558 E AndroidRuntime: java.lang.RuntimeException: Unable to start receiver com.ltlgen.guest_mode.BootReceiver: java.lang.IllegalArgumentException: No such package 

01-17 20:30:43.091  6558  6558 E AndroidRuntime:    at com.ltlgen.guest_mode.MainActivity.handleNotification(MainActivity.java:149)

01-17 20:30:43.091  6558  6558 E AndroidRuntime:    at com.ltlgen.guest_mode.BootReceiver.onReceive(BootReceiver.java:16)

01-17 20:30:43.291  1070  1102 D StatusBarManagerService: manageDisableList userId=0 what=0x0 pkg=Window{4ea5511 u0 Application Error: com.ltlgen.guest_mode}

01-17 20:30:45.331  1070  1692 I ActivityManager: Process com.ltlgen.guest_mode (pid 6558)(adj 15) has died(34,451)

Error: No such package at Line 149

Here is Line 149

RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);

Complete method code is shown below

public void handleNotification()
{
    int mId = 22120927;

    boolean active = isNotificationActive();
    PendingIntent pi = PendingIntent.getService(mContext, 0, new Intent(mContext, MainService.class), PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder n = new NotificationCompat.Builder(mContext);
    n.setOngoing(true)
        .setSmallIcon(R.drawable.ic_launcher);
    if (DEBUG_MODE)
    {
        n.setPriority(NotificationCompat.PRIORITY_MIN);
    }

    RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
    view.setOnClickPendingIntent(R.id.notificationImageButton1, pi);
    n.setContent(view);

    NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    if (active)
    {
        mNotificationManager.notify(mId, n.build());
    }
    else
    {
        mNotificationManager.cancel(mId);
    }
}

BootReceiver.xml

MainActivity main = new MainActivity();
    main.handleNotification();
    main.finish();

AndroidManifest.xnl

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:resizeableActivity = "true">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity> 

    <activity
        android:name=".DialerActivity"
        android:label=""
        android:windowSoftInputMode="stateVisible" />

    <service android:name=".MainService" />

    <receiver
        android:name=".BootReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

</application>


Solution

  • My problem has solved with the help of @Mike M's comment

    GetPackageManager() requires context which was not being initialized when creating MainActivity class Instance manually from BootReceiver

    Now, I have seperated notification handling in MyNotificationManager class as follows:

    public class MyNotificationManager
    {
    private Context mContext;
    
    SharedPreferences mShPrefs;
    SharedPreferences.Editor mShPrefsEditor;
    private static final String KEY_NOTIFICATION_STATUS = "notification_status";
    
    public MyNotificationManager(Context context)
    {
        mContext = context;
    
        mShPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        mShPrefsEditor = mShPrefs.edit();
    }
    
    public void handleNotification()
    {
        int mId = 22120927;
    
        boolean active = isNotificationActive();
        PendingIntent pi = PendingIntent.getService(mContext, 0, new Intent(mContext, MainService.class), PendingIntent.FLAG_CANCEL_CURRENT);
    
        NotificationCompat.Builder n = new NotificationCompat.Builder(mContext);
        n.setOngoing(true)
            .setSmallIcon(R.drawable.ic_launcher);
        if (MainActivity.DEBUG_MODE)
        {
            n.setPriority(NotificationCompat.PRIORITY_MIN);
        }
    
        RemoteViews view = new RemoteViews(mContext.getPackageName(), R.layout.notification);
        view.setOnClickPendingIntent(R.id.notificationImageButton1, pi);
        n.setContent(view);
    
        NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    
        if (active)
        {
            mNotificationManager.notify(mId, n.build());
        }
        else
        {
            mNotificationManager.cancel(mId);
        }
    }
    
    }
    

    And in BootReceiver onReceive()

    public void onReceive(Context context, Intent intent)
    {
        Log.d(TAG, intent.getAction().toString());
    
        MyNotificationManager myNotificationMan = new MyNotificationManager(context);
        myNotificationMan.handleNotification();
    }