从webservice获取数据后是否可以创建通知?

时间:2016-04-07 05:18:18

标签: android notifications

我怀疑是否可以在从webservice获取数据后创建通知需要通过通知管理器创建通知是否可能,因为我无法看到任何有效的工作模型我不喜欢在GCM或其他推送通知中使用我需要使用通知mananger创建通知是否可以让我更准确地解释我的问题当我从webservice到sqlite获取新数据时我需要获得通知如何在没有推送通知方法的情况下实现此目的?

这是webservice代码,我在活动中从服务器获取记录并将其保存在sqlite中:

       public class AccountLiast extends AsyncTask<String, Void, Void> {
            ProgressDialog Dialog = new ProgressDialog(Accounts.this);

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                Dialog.setMessage("Syncing Please Wait");
                Dialog.show();

            }


            @Override
            protected Void doInBackground(String... params) {
                RequestQueue queue = Volley.newRequestQueue(getBaseContext());


                JsonObjectRequest jsonObjRequest = new JsonObjectRequest(Request.Method.GET, params[0], new JSONObject(),
                        new Response.Listener<JSONObject>() {

                            @Override
                            public void onResponse(JSONObject response) {
                                String server_response = response.toString();
                                try {
                                    Model_Account modelobjs = new Model_Account();
                                    JSONObject json_object = new JSONObject(server_response);
                                    JSONArray json_array = new JSONArray(json_object.getString("AccountPageLoadAccountListResult"));for (int i = 0; i < json_array.length(); i++) {

                                        JSONObject json_arrayJSONObject = json_array.getJSONObject(i);
                                        modelobjs.setCompany_group(json_arrayJSONObject.getString("CompanyGroup"));
                                        modelobjs.setParent_company(json_arrayJSONObject.getString("CompanyName"));
                                        modelobjs.setState(json_arrayJSONObject.getString("Region"));
                                        modelobjs.setAccountID(json_arrayJSONObject.getInt("AccountID"));
                                        modelobjs.setCompany_name(json_arrayJSONObject.getString("CompanyName"));
  //This is where i saving it in sqlite db                                    
  account_sf_db.InsertorUpdate(modelobjs);
                                        accountListAdapter.addModelClass(modelobjs);

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

                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Toast.makeText(getBaseContext(), error.toString(), Toast.LENGTH_SHORT).show();

                            }
                        });
                queue.add(jsonObjRequest);
                return null;
            }

            @Override
            protected void onPostExecute(Void modleobjs) {
                super.onPostExecute(modleobjs);


                Dialog.hide();

            }

3 个答案:

答案 0 :(得分:1)

  • 您可以使用内置通知功能

示例

 lapply(Example[setdiff(names(Example), "Sat")], 
       function(x) chisq.test(x, Example$Sat)$residuals)

- 这里我在方法中传递了json response alertMessage。 :

public void generateNotification(String alertMessage) {
        Intent offlineIntent = new Intent(this, NotificationReceiverActivity.class);
        offlineIntent.setAction("Go Offline");

        Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.logo);

        long when = System.currentTimeMillis();

        Uri alarmSound = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        Intent notificationIntent = new Intent(getApplicationContext(), ActivityHome.class);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);


        PendingIntent mainPIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Random random = new Random();
        int m = random.nextInt(9999 - 1000) + 1000;

        Notification noti = new Notification.Builder(this)
                .setContentTitle(getResources().getString(R.string.app_name))
                .setContentText(alertMessage)
                .setSmallIcon(R.mipmap.logo)
                .setLargeIcon(largeIcon)
                .setAutoCancel(true)
                .setSound(alarmSound).setAutoCancel(true).setWhen(when)
                .setContentIntent(mainPIntent)
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
                .build();

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        noti.flags |= Notification.FLAG_AUTO_CANCEL;

        notificationManager.notify(m, noti);

    }

答案 1 :(得分:0)

是的,您可以在从网络服务获取数据后创建通知,

我假设您要在Service获取此数据,因此您可以在下面找到一个在Service

上获取数据的小示例

修改我根据您的需要更改了doInBackground

@Override
        protected Void doInBackground(String... params) {
            RequestQueue queue = Volley.newRequestQueue(getBaseContext());


            JsonObjectRequest jsonObjRequest = new JsonObjectRequest(Request.Method.GET, params[0], new JSONObject(),
                    new Response.Listener<JSONObject>() {

                        @Override
                        public void onResponse(JSONObject response) {
                            String server_response = response.toString();
                            try {
                                Model_Account modelobjs = new Model_Account();
                                JSONObject json_object = new JSONObject(server_response);
                                JSONArray json_array = new JSONArray(json_object.getString("AccountPageLoadAccountListResult"));for (int i = 0; i < json_array.length(); i++) {

                                    JSONObject json_arrayJSONObject = json_array.getJSONObject(i);
                                    modelobjs.setCompany_group(json_arrayJSONObject.getString("CompanyGroup"));
                                    modelobjs.setParent_company(json_arrayJSONObject.getString("CompanyName"));
                                    modelobjs.setState(json_arrayJSONObject.getString("Region"));
                                    modelobjs.setAccountID(json_arrayJSONObject.getInt("AccountID"));
                                    modelobjs.setCompany_name(json_arrayJSONObject.getString("CompanyName"));
                                    //This is where i saving it in sqlite db                                    
                                    account_sf_db.InsertorUpdate(modelobjs);
                                    showNotification(modelobjs); // This is where you will show your notification
                                    accountListAdapter.addModelClass(modelobjs);

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

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(getBaseContext(), error.toString(), Toast.LENGTH_SHORT).show();

                        }
                    });
            queue.add(jsonObjRequest);
            return null;
        }

private void showNotification (Model_Account modelobjs) {
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(modelobjs.getCompany_name());
                    .setContentText("This is notification message");

    Intent resultIntent = new Intent(this, MainActivity.class); //put your activity here

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(mId, mBuilder.build());
    }

答案 2 :(得分:-1)

以下代码段说明了一个简单的通知,指定用户单击通知时要打开的活动。请注意,代码会创建一个TaskStackBuilder对象,并使用它为操作创建PendingIntent。在启动活动时保留导航部分中详细说明了此模式:

NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("My notification")
    .setContentText("Hello World!");
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, ResultActivity.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
   TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
   //Adds the back stack for the Intent (but not the Intent itself)
   stackBuilder.addParentStack(ResultActivity.class);
   // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
    stackBuilder.getPendingIntent(
        0,
        PendingIntent.FLAG_UPDATE_CURRENT
    );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 // mId allows you to update the notification later on.
 mNotificationManager.notify(mId, mBuilder.build());

这是创建简单通知的示例,将此代码段设置为自定义您需要显示的位置。

详细了解通知:Notification | Android

相关问题