【问题标题】:click notification to open new activity点击通知打开新活动
【发布时间】:2018-05-27 15:24:58
【问题描述】:

我的应用中有一条通知,其中包含以下代码: 我的通知很好,但我的问题是我应该做什么,以便在单击我的通知后开始我的主要活动。谢谢。 代码:

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

【问题讨论】:

标签: android push-notification


【解决方案1】:

你需要添加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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多