推送通知无效 - Android

时间:2015-12-18 10:35:10

标签: java android push-notification android-pendingintent

我正在尝试在按钮点击事件上发送推送通知。但不知道为什么,但它不起作用。但它没有显示任何错误。任何人都可以在代码中发现任何错误吗?

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");
}

这是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>

2 个答案:

答案 0 :(得分:1)

改变这个:

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

用这个:

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

答案 1 :(得分:0)

Insted of

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

使用

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