2016-04-07 2 views
0

Ich habe Zweifel, ob ich Benachrichtigung erstellen kann, nachdem Daten vom Webservice eine Benachrichtigung über den Notification Manager erstellt haben, ist möglich, weil ich kein funktionierendes Modell sehen kann, das ich nicht gerne in GCM oder anderen Push-Benachrichtigungen brauche Benachrichtigung erstellen mit Benachrichtigungsmananger Ist es möglich, lassen Sie mich mein Problem genauer erklären, wenn ich neue Daten vom Webservice zu SQLite bekomme ich brauche Benachrichtigung, wie kann ich dies ohne Push-Benachrichtigungsmethode erreichen?Ist es möglich, nach dem Abrufen von Daten vom Webservice eine Benachrichtigung zu erstellen?

Dies ist der Webservice-Code, wo ich Datensatz vom Server in Aktivität zu holen und es in SQLite zu speichern:

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

      } 
+0

was tun Sie in Benachrichtigung –

Antwort

1
  • Sie können Benachrichtigungsfunktionalität

Beispiel

verwenden eingebauten
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); 

    } 

- Hier übergebe ich die json Antwort alertMessage in method. :

+0

Erstmal Danke zeigen wollen NikPatel meine primären Zweifel kann ich Service verwenden, um Daten von Webservice Abrufen und benachrichtigt oder kann ich in Benachrichtigungsmethode in Tätigkeit aufgebaut Ort selbst –

+0

meine bearbeitet am überprüfen –

+0

Sie können die integrierte Benachrichtigungsmethode in der Aktivität selbst verwenden. und Sie können JSON-Antwort in generateNotification-Methode –

-1

Das folgende Snippet veranschaulicht eine einfache Benachrichtigung, die eine Aktivität angibt, die geöffnet werden soll, wenn der Benutzer auf die Benachrichtigung klickt. Beachten Sie, dass der Code ein Objekt TaskStackBuilder erstellt und es zu dem Erstellen der PendingIntent für die Aktion verwendet. Dieses Muster wird im Detail im Abschnitt Navigation Preserving beim Starten eine Aktion:

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

dies ist ein Beispiel für die Erstellung von einfacher Benachrichtigung dieses Snippet setzen und anpassen, wo immer Sie zeigen müssen.

mehr über Benachrichtigung lesen: Notification | Android

0

Ja Sie Ihre Meldung erstellen können, nachdem Sie Daten von Ihrem Webservice erhalten,

Ich nehme an, Sie sind diese Daten bei Service bekommen, so unter Ihnen ein kleines Beispiel finden dass Abfragen von Daten auf Service

bearbeiten ich änderte Ihre doInBackground wie Ihre Bedürfnisse

@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()); 
    } 
+0

am Abrufen und Speichern in sqlite-Datenbank, sobald ich den Datensatz aus dem Dienst abrufen muss benachrichtigt werden, wie kann ich das tun –

+0

Dann wie kann ich dies analysieren –

+0

Sie müssen mir Ihren Dienst zeigen, whre holst du Codes und wo fügst du deine Aufzeichnungen zu sqlite hinzu. Dann kann ich Ihnen besser helfen –

Verwandte Themen