android接收推送,Android-接收并显示推送通知-不太了解

好的,我终于设置好要注册用于推送通知的设备.我发现此代码可以接收新的通知并显示它.事情是不确定的.我是Android编程的新手,因此不胜感激.

我在下面有一个称为GCMService的服务类.

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

}

}

下面的函数与我上面的类相关的地方在哪里呢?

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值