安卓:推送通知

编程入门 行业动态 更新时间:2024-10-19 16:24:03
本文介绍了安卓:推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我开发的应用程序。在该应用程序我使用GCM(推送通知)。

I am developing an application. In that application i am using the GCM (Push Notification).

我把这种帮助链接1 。

和我成功地实施了客户端code。

and i successfully implemented the client side code.

现在发送通知我使用下面的链接为:

Now to send notification i am using the following link as :

链接2

但问题是,我强麦得到通知,但是当我在,没有什么发生点击

But the issue is that i am ale to get the notification, but when i click on that nothing happens.

我应该做的,所以如果通知的网页或更新网页等用户点击打开。

What should i do, so that if user click on notification web page or update page etc opens.

我用同样的code。用在给定的链接。做任何更改到目前为止吧。

I used the same code as given in that link. No changes done so far now.

我在我的应用程序的中间。所以,请指导我,给您的宝贵建议。

I am in middle of my app. So please guide me and give your valuable suggestions.

推荐答案

这个功能在您的主要活动电话。

this function is call in your main activity.

public void registerClient() { try { // Check that the device supports GCM (should be in a try / catch) GCMRegistrar.checkDevice(viewLogin); // Check the manifest to be sure this app has all the required // permissions. GCMRegistrar.checkManifest(viewLogin); // Get the existing registration id, if it exists. regId = GCMRegistrar.getRegistrationId(viewLogin); if (regId.equals("")) { registrationStatus = "Registering..."; // register this device for this project GCMRegistrar.register(viewLogin, GCMIntentService.PROJECT_ID); regId = GCMRegistrar.getRegistrationId(viewLogin); registrationStatus = "Registration Acquired"; // This is actually a dummy function. At this point, one // would send the registration id, and other identifying // information to your server, which should save the id // for use when broadcasting messages. } else { registrationStatus = "Already registered"; } Log.d(TAG, regId); sendRegistrationToServer(); } catch (Exception e) { e.printStackTrace(); registrationStatus = e.getMessage(); } Log.d(TAG, registrationStatus); }

公共类GCMIntentService扩展GCMBaseIntentService {

public class GCMIntentService extends GCMBaseIntentService {

public static final String PROJECT_ID = "566655788"; private static final String TAG = "GCMIntentService"; ModelNotificationMessage modelNotificationMessage; public GCMIntentService() { super(PROJECT_ID); Log.d(TAG, "GCMIntentService init"); } @Override protected void onError(Context ctx, String sError) { // TODO Auto-generated method stub Log.d(TAG, "Error: " + sError); } @Override protected void onMessage(Context ctx, Intent intent) { Log.d(TAG, "Message Received"); String message = intent.getStringExtra("message"); try { modelNotificationMessage = JsonParserNotificationMessage .parserString(message); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.d(TAG, "Message Received" + message); sendNotification(message); Intent broadcastIntent = new Intent(); broadcastIntent.setAction("GCM_RECEIVED_ACTION"); broadcastIntent.putExtra("gcm", message); ctx.sendBroadcast(broadcastIntent); } private void sendNotification(String message) { // this String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); int icon = R.drawable.notification; CharSequence tickerText = message; // ticker-text long when = System.currentTimeMillis(); Context context = getApplicationContext(); CharSequence contentTitle = modelNotificationMessage.getKey(); CharSequence contentText = message; Intent notificationIntent = null; int NOTIFICATION_ID = 9999; try { NOTIFICATION_ID = CommonVariable.notification_Limit; notificationIntent = new Intent(this, ViewLimit.class); contentText = "Limit received for " + modelAgents.getName() + "."; tickerText = "Limit received for " + modelAgents.getName() + "."; } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } // and this PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Notification notification = new Notification(icon, tickerText, when); // Play default notification sound notification.defaults |= Notification.DEFAULT_ALL; notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); mNotificationManager.notify(NOTIFICATION_ID, notification); } @Override protected void onRegistered(Context ctx, String regId) { // TODO Auto-generated method stub // send regId to your server Log.d(TAG, regId); } @Override protected void onUnregistered(Context ctx, String regId) { // TODO Auto-generated method stub // send notification to your server to remove that regId } }

// JSON解析器类

//json parser class

public class JsonParserNotificationMessage { private static final String KEY = "Key"; private static final String BODY = "Body"; public static ModelNotificationMessage parserString(String jsonStrng) throws JSONException { JSONObject jObject = new JSONObject(jsonStrng); ModelNotificationMessage modelNotificationMessage = new ModelNotificationMessage(); if (jObject != null) { modelNotificationMessage.setKey(jObject.getString(KEY)); modelNotificationMessage.setBody(jObject.getString(BODY)); } return modelNotificationMessage; } }

我希望这是对您有用...如果您有任何疑问然后告诉me.this code suceessfully工作在我的应用程序。

i hope it is useful for you... if you any query then tell me.this code suceessfully work in my app.

更多推荐

安卓:推送通知

本文发布于:2023-11-27 08:03:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637201.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!