如何从服务发送通知?

时间:2017-02-12 11:46:43

标签: android service notifications

Hai我尝试让应用程序可以显示来自服务的通知,但为什么通知仅在应用程序打开时显示?我的服务正好开始,我对此一无所知。

这是我的代码:

 public class ServiceBaru extends Service {
    SharedPreferences sharedPreferences3;
    SharedPreferences.Editor editor3;
    SharedPreferences sharedPreferences2;
    SharedPreferences.Editor editor2;
    Timer time = new Timer();
    String url_baru, url_default, url_pakai;
    int idl_modif, ids_modif;
    static String idl = "";
    static String ids = "";
    Context mContext;
    public ServiceBaru() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Handler handler = new Handler();
        sharedPreferences2 = getSharedPreferences("semua_url", MODE_PRIVATE);
        url_baru = sharedPreferences2.getString("url_masuk", "");
        sharedPreferences3 = getSharedPreferences("id_keluar", MODE_PRIVATE);
        editor3 = sharedPreferences3.edit();
        url_default = "192.168.1.215";
        if(url_baru.equals("")){
            url_pakai = url_default;
        }
        else {
            url_pakai = url_baru;
        }
           handler.postDelayed(new Runnable() {

            @Override
            public void run() {
     DapatId getid = new DapatId();
                getid.execute();
            }

        }, 1000);


        System.out.println("Hallo Dari Services");
        return START_STICKY;
    }

  public class DapatId extends AsyncTask<Void, Void, String>{

        @Override
        protected String doInBackground(Void... params) {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().url("http://" + url_pakai + "/www/index.php/ambilid").build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    //Sengaja Kosong
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    //idl Menunjukkan Id Baru
                    // ids menunjukkan id lama dari shared preferences
                    sharedPreferences3 = getSharedPreferences("id_keluar", MODE_PRIVATE);
                    editor3 = sharedPreferences3.edit();
                    idl = response.body().string();
                    idl_modif = Integer.parseInt(idl);
                    ids_modif = sharedPreferences3.getInt("idpertama", 0);
                    if(ids_modif==0) {
                        ids = idl;
                        ids_modif = Integer.parseInt(ids);
                        editor3.putInt("idpertama", ids_modif);
                        editor3.commit();
                    }
                    else{
                        if(idl_modif>ids_modif){
                            NotificationCompat.Builder notif = new NotificationCompat.Builder(getApplicationContext()).setSmallIcon(R.drawable.logoaaagunung).setContentTitle("ggahaMobile").setContentText("Ada Data Baru Yang Perlu Approve");
                            Intent notifklik = new Intent(getApplicationContext(), MainActivity.class);
                            PendingIntent contentnotif = PendingIntent.getActivity(getApplicationContext(), 0, notifklik, PendingIntent.FLAG_UPDATE_CURRENT);
                            notif.setContentIntent(contentnotif);
                            NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                            notificationManager.notify(1, notif.build());
                            editor3.putInt("idpertama", idl_modif);
                            editor3.commit();
                            System.out.println(idl_modif + ids_modif);
                        }
                    }
                }
            });

            return null;
        }

    }

和我的活动:

 Intent service = new Intent(SplashScreen.this, ServiceBaru.class);
                                startService(service);

这是我的logcat:

> W/System.err:     at android.os.AsyncTask.finish(AsyncTask.java:632)
> W/System.err:     at android.os.AsyncTask.access$600(AsyncTask.java:177) W/System.err:     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
> W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102) W/System.err:     at android.os.Looper.loop(Looper.java:136) 
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5021)
> W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method) W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515) W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
> W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)

感谢您的帮助

0 个答案:

没有答案