user1242617
user1242617

Reputation:

Push notifications don't work

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

Upvotes: 2

Views: 5683

Answers (6)

jonathanrz
jonathanrz

Reputation: 4296

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

Upvotes: 0

David Douglas
David Douglas

Reputation: 10503

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>

Upvotes: 1

Eran
Eran

Reputation: 393851

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.

Upvotes: 0

JimTim
JimTim

Reputation: 76

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...)

Upvotes: 0

Ronak Mehta
Ronak Mehta

Reputation: 5979

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?

enter image description here

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

Upvotes: 1

Shadow
Shadow

Reputation: 6899

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/

Upvotes: 3

Related Questions