栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在我自己的Android应用中添加推送通知

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在我自己的Android应用中添加推送通知

我正在发布Google Cloud
Messaging的
演示应用程序。

确保您创建的演示应用程序的API级别等于或高于 具有Google API的Android OS 2.2

用户必须至少登录一个Google帐户才能使用此服务。

首先,您必须添加GCM库。

比在我命名的类上创建

GCMIntentService
扩展了GCMbaseIntentService的类如下:

package com.example.gcmdemo;import android.content.Context;import android.content.Intent;import android.util.Log;import com.google.android.gcm.GCMbaseIntentService;import com.google.android.gcm.GCMConstants;public class GCMIntentService extends GCMbaseIntentService {     private static final String TAG = "Push Notification Demo GCMIntentService";    @Override    protected void onError(Context context, String errorId) {        if(GCMConstants.ERROR_ACCOUNT_MISSING.equalsIgnoreCase(errorId)) { Log.v(TAG, "Error Account Missing");        } else if(GCMConstants.ERROR_AUTHENTICATION_FAILED.equalsIgnoreCase(errorId)) { Log.v(TAG, "Error Authentication Failed");        } else if(GCMConstants.ERROR_INVALID_PARAMETERS.equalsIgnoreCase(errorId)) { Log.v(TAG, "Error Invalid Parameters");        } else if(GCMConstants.ERROR_INVALID_SENDER.equalsIgnoreCase(errorId)) { Log.v(TAG, "Error Invalid Sender");        } else if(GCMConstants.ERROR_PHONE_REGISTRATION_ERROR.equalsIgnoreCase(errorId)) { Log.v(TAG, "Error Phone Registration Error");        } else if(GCMConstants.ERROR_SERVICE_NOT_AVAILABLE.equalsIgnoreCase(errorId)) { Log.v(TAG, "Error Service Not Available");        }     }    @Override    protected void onMessage(Context context, Intent intent) {        // App Server Sends message as key value pairs         String value1 = intent.getStringExtra("key1");        String value2 = intent.getStringExtra("key2");        Log.v(TAG, "key1: "+value1 );        Log.v(TAG, "key2: "+value2 );    }    @Override    protected void onRegistered(Context context, String regId) {        Log.v(TAG, "Successfull Registration : "+regId);    }    @Override    protected void onUnregistered(Context context, String regId) {        Log.v(TAG, "Successfully Unregistred : "+regId);    }    @Override    protected String[] getSenderIds(Context context) {        return super.getSenderIds(context);    }    @Override    protected void onDeletedMessages(Context context, int total) {        super.onDeletedMessages(context, total);    }    @Override    protected boolean onRecoverableError(Context context, String errorId) {        return super.onRecoverableError(context, errorId);    }}

这是在以下演示活动中应如何检查注册的方法:

package com.example.gcmdemo;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.Menu;import com.google.android.gcm.GCMRegistrar;public class MainActivity extends Activity {    private static final String TAG = "Push Notification Demo Activity";    private static final String SENDER_ID = "1069713227710";    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        GCMRegistrar.checkDevice(this);        GCMRegistrar.checkManifest(this);        final String regId = GCMRegistrar.getRegistrationId(this);        if (regId.equals("")) {          GCMRegistrar.register(this, SENDER_ID);        } else {          Log.v(TAG, "Already registered : "+regId);        }    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    }}

最后是演示清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.gcmdemo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="8" />    <permission        android:name="com.example.gcmdemo.permission.C2D_MESSAGE"        android:protectionLevel="signature" />    <uses-permission android:name="com.example.gcmdemo.permission.C2D_MESSAGE" />    <!-- App receives GCM messages. -->    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />    <!-- GCM connects to Google Services. -->    <uses-permission android:name="android.permission.INTERNET" />    <!-- GCM requires a Google account. -->    <uses-permission android:name="android.permission.GET_ACCOUNTS" />    <!-- Keeps the processor from sleeping when a message is received. -->    <uses-permission android:name="android.permission.WAKE_LOCK" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter>     <action android:name="android.intent.action.MAIN" />     <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>        </activity>        <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter>     <action android:name="com.google.android.c2dm.intent.RECEIVE" />     <action android:name="com.google.android.c2dm.intent.REGISTRATION" />     <category android:name="com.example.gcmdemo" /> </intent-filter>        </receiver>        <service android:name=".GCMIntentService" />    </application></manifest>

另外,您将需要此处指定的第三方服务器端脚本。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/427986.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号