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

2024-02-19

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

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android - 接收推送通知并显示它 - 不太理解 的相关文章

随机推荐