0

I am getting the notification perfectly on the device. But it does not open the mentioned Activity. In the log it prints the logs of the mentioned activity but the MainActivity opens. Have referred to the code from Android docs and changed to code according to the answer here https://stackoverflow.com/a/20744397/1928265 . But still not working.

private void sendNotification(String title, String body) {

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

        Intent resultIntent = new Intent(this, NotificationCenterActivity.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

        stackBuilder.addParentStack(NotificationCenterActivity.class);

        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT
                );

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setColor(16734816)
                        .setContentTitle(title)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(body))
                        .setDefaults(NotificationCompat.DEFAULT_ALL)
                        .setContentText(body);

        mBuilder.setAutoCancel(true);
        mBuilder.setContentIntent(resultPendingIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

Android Manifest

<?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="<package name>"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="22" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.VIBRATE"/>

    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR" />

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

    <!--<uses-permission android:name="<package name>.permission.permission.RECEIVE" />-->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="<package name>.permission.C2D_MESSAGE" />
    <permission android:name="<package name>.permission.C2D_MESSAGE" android:protectionLevel="signature" />

    <application
        android:name="com.orm.SugarApp"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/AppTheme"
        tools:replace="android:icon" >
        <meta-data
            android:name="DATABASE"
            android:value="database.db" />
        <meta-data
            android:name="VERSION"
            android:value="1" />
        <meta-data
            android:name="QUERY_LOG"
            android:value="true" />
        <meta-data
            android:name="DOMAIN_PACKAGE_NAME"
            android:value="com.db" />

        <activity
            android:name=".SplashActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        .
        .
        Other Activities
        .
        .

        <activity
            android:name=".NotificationCenterActivity"
            android:screenOrientation="portrait">
        </activity>

        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />


        <receiver
            android:name="<package name>.GcmBroadcastReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="<package name>" />
            </intent-filter>
        </receiver>

        <service android:name="<package name>.GcmIntentService" />

    </application>

</manifest>
Community
  • 1
  • 1
Jay Visariya
  • 83
  • 2
  • 18
  • Paste your manifest. – Madhukar Hebbar Dec 24 '15 at 08:50
  • Have added the manifest. When application is running in background, the specified activity opens but not when application is not running. Can you suggest a way to open a specific activity when application is not running in background? – Jay Visariya Dec 24 '15 at 10:35

2 Answers2

0

Use Like this

Random random = new Random();
int mid = random.nextInt(9999 - 1000) + 1000;

Intent notificationIntent = new Intent(context, landingactivity.class);
PendingIntent intent = PendingIntent.getActivity(context, mid, notificationIntent, PendingIntent.FLAG_ONE_SHOT);

Notification.Builder notification = new Notification.Builder(context)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setLargeIcon(largeicon)
            .setSmallIcon(R.drawable.smallicon_push)
            .setTicker(message);

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

    notification.setContentIntent(intent);

    mNotificationManager.notify(mid, notification.build());

mid is the Random Number to identify your notifications. it should be unique to all the notifications received.

Brendon
  • 1,368
  • 1
  • 12
  • 28
  • You want me to remove TaskStakBuilder and add the Above PendingIntent? I tried this way but it didn't work. Am I doing it correct? – Jay Visariya Dec 24 '15 at 07:31
  • Just use my edited answer, u will be good to go, replace values applicable to your code. – Brendon Dec 24 '15 at 07:40
  • I tried the above code but don't know. It didn't work. I took me to the MainActivity and but got logs of mentioned activity. – Jay Visariya Dec 24 '15 at 07:51
  • Are u clicking the notification when the application is in closed state? i mean the application is not running.. If the application is in running state then the specified activity will trigger for sure, else the default launcher activity only will trigger.. – Brendon Dec 24 '15 at 08:38
  • Yes, it worked when the application is running in background. But is there any other way if we want to direclty open the specified activity? – Jay Visariya Dec 24 '15 at 09:50
  • But why do I get logs of the specified activity even when MainActivity opens up when application is not running in background? – Jay Visariya Dec 24 '15 at 10:36
  • Yes it will, U should change the intent filter to make it open as a launcher activity.. – Brendon Dec 25 '15 at 09:38
0

There could be any issue. I have faced the same problem. Just try these steps. Glad if it works.

  1. Add android:exported="true" for your notification activity .
  2. Change your Pending intent like this PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ONE_SHOT); and also set android:launchMode="singleInstance" in activity manifest.
  3. Add the intent filter to your notification activity. <intent-filter> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
Madhukar Hebbar
  • 3,113
  • 5
  • 41
  • 69
  • Will the above solution solve the issue of opening a specified activity when the application is not working in the background? – Jay Visariya Dec 24 '15 at 10:59