gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:32:24 25 4
gpt4 key购买 nike

我在 Android 中根据本教程开发了一个推送通知应用程序:push notification in android app .运行应用程序时会显示注册按钮。当我点击注册按钮时,当注册成功时,我的设备上会显示一条通知。

如何将它包含在我自己的应用程序中?我的应用程序有一个 xml 解析示例应用程序。在这里,当添加任何新项目时,我希望在设备上显示(显示新订单)通知消息。它在这里自动生成。

最佳答案

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

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

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

首先你必须添加GCM library .

比在我命名为 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);
}
}

以下是在以下演示 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;
}
}

最后是演示 list :

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

您还需要third party server side script as specified here .

关于java - 如何在我自己的 Android 应用程序中添加推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12560246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com