Pie
Pie

Reputation: 11

Clicking on Notification in background not open my App?

I am a newbie in android development.

I create a background service for receive pushNotification (with our own socket service, not FCM). And now my device receive message and show notification successfully. But when I click the notification, it not return to my app.

When I click notification, show the error below: ActivityManager: Unable to start service Intent { cmp=com.my.app/package.name.AppActivity } U=0: not found

Here is my code:


    public static void setNotification(Service service, String message) {

        NotificationManager manager = (NotificationManager) service.getSystemService(NOTIFICATION_SERVICE);

        String appName = service.getString(R.string.app_name);
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(service)
                .setContentTitle(appName)
                .setContentText(message) 
                .setContentIntent(getDefaultIntent(service)) 
                .setWhen(System.currentTimeMillis()) 
                .setOngoing(false)
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_VIBRATE) 
                .setSmallIcon(R.drawable.icon) 
                .setLargeIcon(BitmapFactory.decodeResource(service.getResources(), R.drawable.icon)) 
                .setSound(alarmSound); 
        Notification notification = builder.build(); 
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        manager.notify(1, notification);
    }

    private static PendingIntent getDefaultIntent(Service service) {
        Intent intent = new Intent(service, AppActivity.class);
        PendingIntent pendingIntent = PendingIntent.getService(service, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        return pendingIntent;
    }
}

MainActivity setting in AndroidManifest.xml

<activity android:name="package.name.AppActivity" android:configChanges="orientation|screenSize" android:label="@string/app_name" android:launchMode="singleTask" android:screenOrientation="landscape" android:theme= "@android:style/Theme.NoTitleBar.Fullscreen" android:enabled="true" android:exported="true">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>

    </activity>

Please guide me what I am missing or doing wrong in this code. Many thanks.

Upvotes: 0

Views: 783

Answers (2)

Pie
Pie

Reputation: 11

I pass same id in padding intent and manager.notify(0, notification);

And use PendingIntent.getActivity instead of PendingIntent.getService

it works!

Upvotes: 1

Dipankar Baghel
Dipankar Baghel

Reputation: 2039

I got the problem you only cancel the current notification thats why Intent is not firing the activity Change your Pending intent

from

 PendingIntent.FLAG_CANCEL_CURRENT);

to

PendingIntent.FLAG_UPDATE_CURRENT
                        | PendingIntent.FLAG_ONE_SHOT);

Upvotes: 0

Related Questions