【问题标题】:Android GCM Java Server - Push Notification without message dataAndroid GCM Java 服务器 - 没有消息数据的推送通知
【发布时间】:2013-01-28 11:13:41
【问题描述】:

我正在尝试开发 java 服务器以在我的应用中发送推送通知。

我成功发送了通知,但我有两个问题...

  1. 状态栏中不显示消息文本(仅显示应用程序名称)

  2. 当我点击状态栏中的通知时,没有任何反应(未打开应用程序)

这是我的java服务器代码:

    //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();
    }

我在路由器上打开了 5228 端口,但它没有改变...

以及我的应用程序中的来源:

/**
 * 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);   
}

【问题讨论】:

  • @Mica,我需要实现同样的功能。你能帮我实现这个吗?

标签: java android push-notification google-cloud-messaging


【解决方案1】:

【讨论】:

  • 谢谢,我成功正确显示消息,但我仍然无法启动应用程序。在 logcat 我有: I/ActivityManager(326): START {flg=0x4000000 cmp=com.****.****/android.app.LauncherActivity bnds=[0,158][720,286]} from pid -1
猜你喜欢
  • 2012-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多