点击通知打开新活动
Posted
技术标签:
【中文标题】点击通知打开新活动【英文标题】: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);
【问题讨论】:
Start an Activity when clicking a notification的可能重复 @afc11hn 你能编辑我的代码吗 【参考方案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);
【讨论】:
以上是关于点击通知打开新活动的主要内容,如果未能解决你的问题,请参考以下文章