简体   繁体   中英

Android GCM Java Server - Push Notification without message data

I am trying to develop java server to send push notification in my app.

I succeed to send a notification but I have two problems ...

  1. The message text not appear in status bar (only the name of the application is displayed)

  2. When i click on the notification in the status bar nothing happens (not opening the application)

This is my code of the java server:

    //ArrayList<String> userGcmList = new ArrayList<String>();   
    ArrayList<String> userRegIdGcmList = new ArrayList<String>(); 

    //my phone
    userRegIdGcmList.add("***********");        

    int numverOfDevicesRegisterd = userRegIdGcmList.size(); 

    try {                   
        //AI KEY 
        Sender sender = new Sender("*****************");    

        // use this line to send message with payload data
        Message message = new Message.Builder()
            .collapseKey("1")
            .timeToLive(3)
            .delayWhileIdle(true)
            .addData("message", "Text in the status bar does not appear")
            .build();

        // Use this for multicast messages
        MulticastResult result = sender.send(message, userRegIdGcmList, numverOfDevicesRegisterd);
        sender.send(message, userRegIdGcmList, numverOfDevicesRegisterd);           
        System.out.println(result.toString());  

        if (result.getResults() != null) {
            int canonicalRegId = result.getCanonicalIds();
            if (canonicalRegId != 0) {
            }
        } else {
            int error = result.getFailure();
            System.out.println(error);
        }

    } catch (Exception e) {         
        e.printStackTrace();
    }

I open the port 5228 on my router but it does not change...

and the source in my app :

 /** * Issues a notification to inform the user that server has sent a message. */ private static void generateNotification(Context context, String message) { 
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher, message, when);

    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, LauncherActivity.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;
    notificationManager.notify(0, notification);   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM