【问题标题】:Display a message from Api as a notification显示来自 Api 的消息作为通知
【发布时间】:2019-01-07 09:13:48
【问题描述】:

我从我的 Api 获得了一个文本,我想将其显示为通知。我该怎么做?这是我拥有通知代码和获取 json 对象的代码的代码。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pushnotifications);
NotificationCompat.Builder mBuilder =   new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher_background) // notification icon
            .setContentTitle("Notification!") // title for notification
            .setContentText("Hello word"); // message for notification

    Notification mNotificationManager = mBuilder.build();
    NotificationManagerCompat.from(this).notify(0,mNotificationManager);

    URL obj = null;
    try {
        obj = new URL("https://gekon.technologypark.cz/api/v1/notification/demo/8");
        HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("ApiSecret", LoginInfo.ApiSecret);
        conn.connect();
        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
        StringBuilder sb = new StringBuilder();
        String output;
        while ((output = br.readLine()) != null)
            sb.append(output);

        JSONObject jsonObj = new JSONObject(sb.toString());

        String notifText=(String) jsonObj.get("data");

        Log.d("log","PUSH NANI"+notifText);


    } catch (Exception e) {
        e.printStackTrace();
        Log.d("log","PUSH CATCG"+e);

    }

}

【问题讨论】:

  • 如果你在android 8及以上的设备上测试你需要配置通知通道。
  • @KaranMer 我知道,但我正在 android 7 上测试它
  • 您需要在 notify() 中使用随机 id,因为使用重复的 id 会覆盖您的通知

标签: android json api notifications


【解决方案1】:

在api成功调用你的通知代码,看看

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pushnotifications);


    URL obj = null;
    try {
        obj = new URL("https://gekon.technologypark.cz/api/v1/notification/demo/8");
        HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("ApiSecret", LoginInfo.ApiSecret);
        conn.connect();
        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
        StringBuilder sb = new StringBuilder();
        String output;
        while ((output = br.readLine()) != null)
            sb.append(output);

        JSONObject jsonObj = new JSONObject(sb.toString());

        String notifText=(String) jsonObj.get("data");

        Log.d("log","PUSH NANI"+notifText);
         showNotification(notiftext)


    } catch (Exception e) {
        e.printStackTrace();
        Log.d("log","PUSH CATCG"+e);

    }

}

这里是api调用成功后显示通知的方法。

public void showNotification(String message){
NotificationCompat.Builder mBuilder =   new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher_background) // notification icon
                .setContentTitle("Notification!") // title for notification
                .setContentText(message); // message for notification

       NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
       NotificationManager.notify().
      mNotificationManager.notify(001, mBuilder.build());
}

希望对你有帮助!!

【讨论】:

  • 感谢您的回答!奇怪的是它没有显示任何通知:/
  • @AlexKolydas 我编辑了代码,请再次检查!
  • 还是什么都没有:/ 我删除了 NotificationManager.notify()。因为我认为这是一个错误,对吧?
  • Log.d("log","PUSH NANI"+notifText); 它的打印是什么?
  • 我的 api 文本
猜你喜欢
  • 2019-07-02
  • 1970-01-01
  • 1970-01-01
  • 2021-07-28
  • 2019-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多