6

I am trying to implement push notification on my android app. So no better place to start then the Googles Developers page.

I am trying to follow this tutorial here : GCM Demo App . Tutorial suggests to use the sample code that is given through the SDK Manager. After doing so and trying to send a push notification , when the app is running i see on the screen being written that a new push has arrived.

However , when the app is on background or not running i dont get a push notification. If i open the app , again the messages are shown on the screen. But i never get anything in the form of notification with a popup and a sound.

Do i manually have to do this in android? I thought it would be similar to iOS where the platform is responsible for showing you the notification.

Any ideas how i can implement it?

donparalias
  • 1,834
  • 16
  • 37
  • 60

2 Answers2

5

But i never get anything in the form of notification with a popup and a sound.

The sound can be programmed into the code when a notification is triggered. Say with something like this.

Notification notification = new Notification(icon, tickerText, when);
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

However , when the app is on background or not running i dont get a push notification

Do i manually have to do this in android? I thought it would be similar to iOS where the platform is responsible for showing you the notification.

Your app is always sent the GCM data (push notifications). How you handle that data is up to you. The GCM intent service is responsible for delivering data to you and that is it. You will need to use the notification service to show the appropriate notification to the user.

There are advantages / disadvantages to this approach. Application code will run on android when you receive push notifications, which is not the case on the iPhone. You also have the flexibility to silently make updates or notify the user based on the type of push notification.

Register your device with the sender ID when your app starts up and you should receive notifications as expected. All push notifications will be delivered to this method protected void onMessage(Context context, Intent intent) on the GCMIntentService of your choice.

Deepak Bala
  • 11,095
  • 2
  • 38
  • 49
  • Can you help me how to fix it though? I need the notification to show up and play a sound. The 2 lines are all the code i should implement? – donparalias Mar 26 '13 at 09:52
  • 1
    It is a little more involved than that. To send a notification, take a look at the code in this question - http://stackoverflow.com/questions/7040742/android-notification-manager-having-a-notification-without-an-intent As for the notification pop up you can pass the GCM message to a BroadcastReceiver registered to your activity which can then show an alert dialog. Example code for alert dialog - http://www.mkyong.com/android/android-alert-dialog-example/ – Deepak Bala Mar 26 '13 at 10:01
  • 1
    @DeepakBala Your answer says something about silent notification on android. I am working on Android's silent push notification thing, where if the user is already logged in to the app. The app needs to know that it is subscribed to the channel. However the user doesn't need to be bothered with any notification. Do you have any idea how can I do this? – silent_programmer Mar 26 '15 at 17:45
  • @AtharvaPuranik yup. Track whether the activity is visible or invisible to the user and use this flag on the class that receives the GCM message to decide whether a notification should be generated. A working example is available on the android sdk that my company makes for a 2 way in-app messaging inbox - https://github.com/deepak-bala/konotor-android. The repo does not have the code but I can share a snippet if you create a question. – Deepak Bala Mar 27 '15 at 01:54
  • @DeepakBala Here is the link for my newly posted question: http://stackoverflow.com/questions/29305176/android-silent-push-notification Thanks! – silent_programmer Mar 27 '15 at 16:09
3

Try to unregister and register your device again. In DemoActivity.java put

final String regId = GCMRegistrar.getRegistrationId(this);
        GCMRegistrar.unregister(this);

then ,remove GCMRegistrar.unregister(this); in second launch.

Update

Notifications in your Application:

Create class

public class DemoApplication extends Application {

    private class NotifyReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
              Toast.makeText(context, "RECEIVE MESSAGE", Toast.LENGTH_SHORT).show();
        }
    }

    private NotifyReceiver notifyReceiver = new NotifyReceiver();

    @Override
    public void onCreate() {
        registerReceiver(notifyReceiver, new IntentFilter("GCM_MESSAGE"));
        super.onCreate();
    }

    @Override
    public void onTerminate() {
        unregisterReceiver(notifyReceiver);
        super.onTerminate();
    }
}

Then put

<application
           android:name=".DemoApplication"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" > 

in AndroidManifest.xml and send broadcast in

  @Override
    protected void onMessage(Context context, Intent intent) {
        Log.i(TAG, "Received message");
        String message = getString(R.string.gcm_message);
        displayMessage(context, message);
      context.sendBroadcast(new Intent("GCM_MESSAGE"));
        // notifies user
        generateNotification(context, message);
    }

As alternative case you can register broadcastReceiver in Manifest ,Activity or ForeGround Service

Yahor10
  • 2,123
  • 1
  • 13
  • 13
  • Why? What do you have in mind and you think that could help? And even if it helped wouldnt it be scary , having in mind that this is going to be a production app and the push wont work?! – donparalias Mar 26 '13 at 09:43
  • It should not be scary when you just edit incorrect registration on your server. That will not happened in production application – Yahor10 Mar 26 '13 at 09:47
  • and why the registration was incorrect in the first place? What happens if a 10000 users get an incorrect registration? They will never get my notifications? – donparalias Mar 26 '13 at 09:50
  • Monitor your users with The 3rd-party server.Then when 10000 users Idle a long time you should notify him about gcm service error – Yahor10 Mar 26 '13 at 09:52
  • I think you got it wrong man. I get the message... When i open the app all the messages are there.. The problem is that i dont get it in a NOTIFICATION. No pop ups and sounds to inform the user. The message as i described is there , it comes in a second , but without the notification bubble. – donparalias Mar 26 '13 at 09:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26923/discussion-between-donparalias-and-yahor10) – donparalias Mar 26 '13 at 10:36
  • @Yahor10 will u suggest something on this question http://stackoverflow.com/questions/18571844/gcm-not-sending-the-notifications – Developer Sep 02 '13 at 13:26