1

I have a simple android app to display a notification on screen. The app runs properly till builder.setContentIntent() and displays the Toast message properly. If the line, manager.notify() is not commented the App stops. I am using Android Studio.

MainActivity.java

here is my code for notifications

private void addNotification() {
        message = txtMessage.getText().toString();
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setContentTitle("You have a new message").setContentText(message);
        // Add as notification
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Intent notificationIntent = new Intent(this, MainActivity.class);

       PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);

        Toast.makeText(getApplicationContext(), "contentIntentset",
                Toast.LENGTH_SHORT).show();

        //manager.notify(0, builder.build());

}

AndroidManifest.xml

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".NotificationView"
            android:label="Details of notification"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
    </application>

Can someone help?

Sahitya Sridhar
  • 119
  • 3
  • 10

0 Answers0