Search code examples
javaandroidpush-notificationandroid-pendingintent

Push notification not working - Android


I am trying to send a push notification on a button click event. But don't know why, but it is not working. It doesn't show any error though. Can anybody find any error in the code?

public class Verify extends AppCompatActivity {

public static String TAG=Verify.class.getSimpleName();
NotificationCompat.Builder notification;
private static final int uniqueID=12345;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_verify);
    notification=new NotificationCompat.Builder(this);
    notification.setAutoCancel(true);
}

public void onVerify(View v)
{
    this.defineNotification();
//other code follows
}

public void defineNotification()
{
    notification.setContentTitle("Successfully Signed Up");
    notification.setContentText("Hi, you just Signed Up as a Vendor");
    notification.setWhen(System.currentTimeMillis());

    Intent intent=new Intent(this,OtherActivity.class);
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setContentIntent(pendingIntent);

    NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(uniqueID,notification.build());
    //Log.i(TAG, "coming here in notification");
}

Here is the Android Manifest Code :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sachinparashar.xyz">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Verify"
        android:label="@string/title_activity_verify"
        android:theme="@style/AppTheme"/>
    <activity
        android:name=".VendorDetails"
        android:label="Vendor Details"
        android:theme="@style/AppTheme"/>
    <activity
        android:name=".OrderTypes"
        android:label="Orders"
        android:theme="@style/AppTheme" />

</application>


Solution

  • change this :

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    with this :

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);