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

标签 android android-intent push-notification google-cloud-messaging

好的,我终于设置好为推送通知注册设备。我发现此代码可以接收新通知并显示它。问题是我不确定它会去哪里。我是 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);
}

最佳答案

我想通了。我终于找到了一个非常好的工作项目,您可以研究它的代码并理解它。我花了好几天的时间在谷歌上搜索才找到了一些东西,它不仅仅是这里的一 block 和那里的一 block ,而是整个项目。我想我会很好地发布它以防其他人可以使用它。

https://github.com/Guti/Google-Cloud-Messaging--Titanium-

关于Android - 接收推送通知并显示它 - 不太了解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15996695/

相关文章:

Android 退出自己的 Launcher App

android - 增加 Android 模拟器对一般/应用程序操作的响应时间

web - 渐进式网络应用程序的离线推送通知

ios - 如何发送静默推送通知负载

Android:如何设置 Activity 窗口的绝对大小?

android - R.id 无法解析或不是字段。

android Charge intent 没有额外数据

android - 从广播 Intent 中获取 Parcelable 数据时出现 ClassNotFoundException

java - Android 服务 - 重新绑定(bind)时 Intent 不会更新

iOS 8 推送通知检查返回错误的 UIRemoteNotificationType