单击通知以打开新活动

时间:2018-05-27 15:24:59

标签: android push-notification

我的应用程序中有一个通知,其中包含以下代码: 我的通知非常好,但我的问题是我应该怎么做,以便在点击我的通知后启动我的主要活动。谢谢。 代码:

public void run_loop(){
    final Globalv globalv = (Globalv) getApplicationContext();
    RequestQueue requestQueue;
    String url = "https://......./show.php";
    requestQueue = Volley.newRequestQueue(this);
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null ,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("allmess");
                        JSONObject respons = jsonArray.getJSONObject(0);
                        String id = respons.getString("id");
                        int lastThread=  Integer.parseInt(String.valueOf(id));
                        if (globalv.getTotal_threads() < lastThread) {
                            globalv.setTotal_threads(lastThread);
                            NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
                            builder.setContentTitle("هلالية للغات");
                            builder.setContentText("رسالة جديدة");
                            builder.setSmallIcon(R.drawable.splash_img);
                            builder.setAutoCancel(true);
                            builder.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE);
                            builder.setNumber(1);
                            Notification notification = builder.build();
                            NotificationManager mm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                            mm.cancel(1);
                            mm.notify(1, notification);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("VOLLEY", "ERROR");
        }
    }
    );
    requestQueue.add(jsonObjectRequest);
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            run_loop();
        }
    }, 29000);
}

1 个答案:

答案 0 :(得分:0)

您需要添加setContentIntent:

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setContentTitle("هلالية للغات");
builder.setContentText("رسالة جديدة");
builder.setSmallIcon(R.drawable.splash_img);
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE);
builder.setNumber(1);

Intent notificationIntent = new Intent(getApplicationContext(), StartActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
builder.setContentIntent(contentIntent);

Notification notification = builder.build();
NotificationManager mm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mm.cancel(1);
mm.notify(1, notification);