簡體   English   中英

Android GCM Java Server - 無消息數據的推送通知

[英]Android GCM Java Server - Push Notification without message data

我正在嘗試開發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);   
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM