【问题标题】:GCM Notification shows JSON messageGCM 通知显示 JSON 消息
【发布时间】:2014-08-20 11:08:18
【问题描述】:

我面临创建 GCM 通知的问题。默认情况下,它以 JSON 形式显示消息。

我想包装我的 JSON,只想在通知中显示一些消息,比如“你收到了新消息”,但是当任何用户点击通知时,其他信息 JSON 等应该可用于客户端应用程序。

用 Java 构建 GCM 服务器应用程序并在 tomcat 上运行。

我遵循了 GCM 官方网站教程和他们的演示客户端/服务器应用程序。

[更新]:

Sender sender = new Sender(Constants.SENDER_ID);            
androidTargets.add(MY_DEVICE_ID);

Message message = new Message.Builder()
            .collapseKey("GCM_Message")
            .timeToLive(30)
            .delayWhileIdle(true)
            .addData("message", msg)
            .build();          

MulticastResult result = sender.send(message, androidTargets, 3);

System.out.println("GCM message sent");
if (result.getResults() != null) {
    int canonicalRegId = result.getCanonicalIds();
    if (canonicalRegId != 0) {  

    }
} else {
    int error = result.getFailure();
    System.out.println("Broadcast failure: " + error);
}

而 'message' 是 json 字符串。

我们将不胜感激。

【问题讨论】:

  • 告诉我们你到目前为止都做了什么!
  • @goonerdroid 请查看更新,提到从 GCM 服务器发送 GCM 的代码

标签: java android json notifications google-cloud-messaging


【解决方案1】:

将您的主题字符串传递给 setContentTitle(subject), 您可以检查此代码 sn-p 它可能会对您有所帮助。

NotificationManager notificationManager = (NotificationManager) parametes.context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intent = null;
        intent = new Intent(parametes.context, MainActivity.class);

        PendingIntent contentIntent = PendingIntent.getActivity(
                parametes.context, 0, intent, 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                parametes.context)
                .setSmallIcon(R.drawable.app_icon)
                .setContentTitle("My Subject Line here.")
                .setAutoCancel(true);


        mBuilder.setContentIntent(contentIntent);
        parametes.mNotificationManager.notify(
                CheckInboxWithCountParams.NOTIFICATION_ID,
                mBuilder.getNotification());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 2016-07-24
    相关资源
    最近更新 更多