Push notification on Android

2019-08-27 21:51发布

问题:

I create an application on which I want to implement Push Notifications. Thanks to many tutorials I've create the server part and the client part.

On the server part, I send a POST request with cURL (in PHP). The server responds: {"multicast_id":5560733296047502303,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1408610711700937%6f027011f9fd7ecd"}]}

But I don't receive the notification on my phone.

In the application I have a service: Manifest.xml

<service android:name=".GCMIntentService" />

And all needed permissions.

GCMIntentService.java

protected void onMessage(Context context, Intent intent) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();

    NotificationManager notificationManager = (NotificationManager)
    context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, MainActivity.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
    Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent =
    PendingIntent.getActivity(context, 0, notificationIntent, 0);      
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    //notification.sound = Uri.parse("android.resource://"+ context.getPackageName()+ "your_sound_file_name.mp3");
    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);        
}

How can I debug this? Or is there any obvious reason?

回答1:

On Device, you need to perform following steps :

  1. Register to GoogleCloudMessaging.

    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context); String gcmRegistrationId = gcm.register();

  2. Send gcmRegistrationId got in step 1 to Server. Server will use this id to send GCM tickle.

  3. Register GCM Receiver. Add it in AndroidManifest.xml as below :

<receiver
    android:name="com.hp.msa.receiver.GCMReceiver"
    android:exported="true"
    android:permission="com.google.android.c2dm.permission.SEND" >
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
    </intent-filter>
</receiver>

GCMReceiver.java will look as below :

public class GCMReceiver extends WakefulBroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

        // Here, you will get actual PUSH notification.
        // After receiving it, you can perform your tasks
        // Intent contains data sent by server  

            GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

            // The getMessageType() intent parameter must be the intent you received
            // in your BroadcastReceiver.

            String messageType = gcm.getMessageType(intent);  

        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                // Logic
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                // Logic
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // Logic
        }
       }
}


回答2:

The process of sending and receiving push notifications is rather complex as it involves several parties. If you are using google server to broadcast your request (which I think you should be doing), make sure that you server IP is added to the push notification in google API console. If your phone is registering with a push server, it should receive some kind of intent within a minute after sending the push. You can try to debug it, connect your phone to PC, connect debugger, remove all filters and in the debug console of you IDE look for any intent occurrence that could be google push notification. If you receive any, that means that you application is unable to receive the injected intent.

You can checkout aerogear project, which provides a ready push server which is able to register phone installations and forward notifications to google server. It also provides basic web console for managing the server. Aerogear web site also contains many tutorials and examples of working apps for android and other platforms.

http://aerogear.org/

https://github.com/aerogear/aerogear-push-helloworld/tree/master/android

Good luck! Push notifications take some time but not because of the complexity but the amount of things you have to do.