Android的 - 接收推送通知,并显示它 - 不是很了解 [英] Android - Receive Push notification and display it - not quite understanding
问题描述
好了,所以我终于建立以注册推送通知的设备。我发现这个code接收新的通知,并显示它。事情是我不知道通向哪里。我是pretty新到Android编程,因此任何帮助是AP preciated。
我有一个名为下面GCMService服务类。
进口android.content.Context;
进口android.content.Intent;
进口android.util.Log;进口com.google.android.gcm.GCMBaseIntentService;公共类GCMService扩展GCMBaseIntentService { 私有静态最后弦乐TAG =GCMService; 公共GCMService(){
超();
} @覆盖
保护无效onRegistered(上下文的背景下,字符串REGID){
Log.i(TAG,设备注册:REGID =+ REGID);
} @覆盖
保护无效onUnregistered(上下文的背景下,字符串REGID){
Log.i(TAG,设备未注册);
} @覆盖
保护无效的onMessage(上下文的背景下,意图意图){
Log.i(TAG,收到消息);
} @覆盖
公共无效onerror的(上下文的背景下,字符串ErrorID中){
Log.i(TAG,收到的错误:+ ErrorID中);
} @覆盖
保护布尔onRecoverableError(上下文的背景下,字符串ErrorID中){
Log.i(TAG获得恢复的错误:+ ErrorID中);
返回super.onRecoverableError(背景下,ErrorID中);
}
}
哪里以下功能关系到我的课上面收到新信息?
私有静态无效generateNotification(上下文的背景下,字符串消息){ INT图标= R.drawable.ic_launcher;
时长= System.currentTimeMillis的(); NotificationManager notificationManager =(NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE); 通知通知=新的通知(图标,消息,时); 字符串title = context.getString(R.string.app_name);
意图notificationIntent =新意图(背景下,LauncherActivity.class); 的PendingIntent pintent = PendingIntent.getActivity(背景下,0,意向,
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); notification.setLatestEventInfo(上下文,标题,邮件,意图);
notification.flags | = Notification.FLAG_AUTO_CANCEL;
notification.defaults | = Notification.DEFAULT_SOUND;
notification.defaults | = Notification.DEFAULT_VIBRATE;
notificationManager.notify(1,通知);
}
我想通了。我终于找到了可以学习code和理解一个pretty良好的工作项目。我花了几天google搜索找到的东西,不只是在这里一块,一块那里,而是一个整体的项目。我想我是不够好,它张贴以防别人可以使用它。
https://github.com/Guti/Google-Cloud-Messaging- -Titanium-
Ok so I finally got setup to register devices for push notifications. I found this code to receive new notifications and display it. Thing is I'm not sure where it goes. I'm pretty new to Android programming so any help is appreciated. I have a service class called GCMService below.
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMService extends GCMBaseIntentService {
private static final String TAG = "GCMService";
public GCMService() {
super();
}
@Override
protected void onRegistered(Context context, String regId) {
Log.i(TAG, "Device registered: regId= " + regId);
}
@Override
protected void onUnregistered(Context context, String regId) {
Log.i(TAG, "Device unregistered");
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
}
@Override
public void onError(Context context, String errorId) {
Log.i(TAG, "Received error: " + errorId);
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
Log.i(TAG, "Received recoverable error: " + errorId);
return super.onRecoverableError(context, errorId);
}
}
Where does the function below go in relation to my class above to receive a new message?
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, LauncherActivity.class);
PendingIntent pintent = PendingIntent.getActivity(context, 0, intent,
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(1, notification);
}
I figured it out. I finally found a pretty good working project for which you can study the code and understand it. It took me days of googling to find something that wasn't just a piece here and a piece there but instead a whole project. I figured I'd be nice enough to post it in case someone else could use it.
https://github.com/Guti/Google-Cloud-Messaging--Titanium-
这篇关于Android的 - 接收推送通知,并显示它 - 不是很了解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!