Push notifications don't work

萝らか妹 提交于 2019-12-11 02:21:45

问题


I am newbie to android. I had tried the sample given in documentation., but iam not getting any push notification only iam getting the notification icon but no content and title. Can anyone help me in getting cleared??????

  1. Will push notification works in emulator or not?
  2. In this sample i found a class as "Resultactivity". Purpose of using this
    class?

I had given the code below.

package com.example.pushnotification;

import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.view.Menu;

public class Pushactivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pushactivity);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("Check Notification")
            .setContentText("This is to test push!");

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

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(ResultActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int mId = 10;
    mNotificationManager.notify(mId, mBuilder.build());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.pushactivity, menu);
    return true;
}

}

Thanks in advance


回答1:


Surely it will work. I used gcm,it worked in emulator and real device.

You must install google cloud messaging from android sdk manager and use gcm.jar.

This is the best tutorial where you can test it.http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/




回答2:


Will push notification works in emulator or not?

Yes you can test Push Notification in emulator , here is the Source , Here you can find steps to display Push Notification inside emulator , you need to download gcm.jar

From eclipse :

Window => Android SDK Manager => Extras => Google Cloud Messaging for Android Library

In this sample i found a class as "Resultactivity". Purpose of using this class?

Here you can see NotificationCompat.Builder image , which is used to show Notification ,

Visit this Link 1 , Link 2 for more information , when you click on Notification you will be redirected to ResultActivity




回答3:


Check your AndroidManifest.xml file. (replace **manifest-package** with your package name)

<uses-permission android:name="android.permission.INTERNET" />
<permission android:name="**manifest-package**.permission.C2D_MESSAGE"
        android:protectionLevel="signature"/>
<uses-permission android:name="**manifest-package**.permission.C2D_MESSAGE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>

application...

    <receiver android:name=".C2DMMessageReceiver"
              android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />

            <category android:name="**manifest-package**" />
        </intent-filter>
    </receiver>

</application>



回答4:


A few days ago I dealt with gcm and have used the following tutorials:

Simple Tutorial: http://fundroiding.wordpress.com/2012/06/29/google-cloud-messaging-for-android-gcm-simple-tutorial/

If you need a backend, which can use push, then I would recommend a "BaaS" service. The services usually have pre-installed "push modules". (Examples: apiOmat, parse...)




回答5:


GCM will work on an emulator running Android 2.2 with Google APIs.

It requires devices running Android 2.2 or higher that also have the Google Play Store application installed, or or an emulator running Android 2.2 with Google APIs. However, you are not limited to deploying your Android applications through Google Play Store.




回答6:


I had the same problem and found the correction is this question: Clicking on Notification is not starting intended activity?

The correction is to change the line:

PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

to

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


来源:https://stackoverflow.com/questions/16713321/push-notifications-dont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!