如何在我自己的安卓应用中添加推送通知 [英] How to add a push notification in my own android app
问题描述
我根据本教程在 Android
中开发了一个推送通知应用程序:Android 应用程序中的推送通知.运行应用程序时会显示注册按钮.当我点击注册按钮并且注册成功时,我的设备上会显示一条通知.
I have developed a push notification application in Android
from this tutorial:push notification in android app. The register button is displayed when I run the app. When I click on the register button, and when registration is successful, a notification is displayed on my device.
如何将其包含在我自己的应用中?我的应用程序有一个 xml 解析示例应用程序.在这里,当添加任何新项目时,我希望在设备上显示(显示新订单)通知消息.这里是自动生成的.
How can I include it in my own app? My app has one xml parsing example app. Here when any new item is added, I wish to display (the new order is displayed ) a notification message on the device. It is automatically generated here.
推荐答案
我正在发布 Google 的演示应用程序云消息传递.
确保您创建的演示应用程序的 API 级别等于或高于 Android OS 2.2 with Google API
Make sure you create demo application with API level equal or higher than Android OS 2.2 with Google API
用户必须至少登录一个 Google 帐户才能使用此服务.
User have to signed in at-least one Google Account to use this service.
首先,您必须添加 GCM 库.
然后在我命名为 GCMIntentService
的类上创建它扩展 GCMBaseIntentService 如下:
Than create on class which I named GCMIntentService
which extends GCMBaseIntentService as follows:
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);
}
}
以下是您在以下演示活动中检查注册的方法:
Here is how you should check registration in following demo activity :
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>
您还需要此处指定的第三方服务器端脚本.
这篇关于如何在我自己的安卓应用中添加推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!