I had this message when running the project:
Unfortunately the project stopped
also when I add the code between try and catch I had this error
application dosenot define permission com.example.elarabygroup.permission.c2d MESSAGE
I tested all codes and I found the error to be here:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_elaraby_group);
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");
}
}
manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.elarabygroup"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 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=".ElarabyGroup"
android:label="#string/title_activity_elaraby_group" >
<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=".ElarabyGroup" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
</manifest>
GCMIntentService class
package com.example.elarabygroup;
import com.google.android.gcm.GCMBaseIntentService;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class GCMIntenetService extends GCMBaseIntentService {
public static String TAG = "GCMIntentService";
public GCMIntenetService(String senderId) {
super(senderId);
Log.d("GCMIntentService", senderId);
}
#Override
protected void onError(Context arg0, String arg1) {
Log.d("onError", arg1);
}
#Override
protected boolean onRecoverableError(Context context, String errorId) {
Log.d("onRecoverableError", errorId);
return false;
}
#Override
protected void onMessage(Context arg0, Intent arg1) {
Log.d("onMessage", String.valueOf(arg1));
}
#Override
protected void onRegistered(Context arg0, String arg1) {
Log.d("onRegistered", arg1);
}
#Override
protected void onUnregistered(Context arg0, String arg1) {
Log.d("onUnregistered", arg1);
}
}
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Try this
Related
I can receive message google-cloud-messaging data payload when my app is active, but when the app is not active I do not receive messages.
My device is android, when app is aliveļ¼send notification or data message. It worked.
but when app is not alive, sent data message, it doesn't work.
Is there any way to use the data payload message to activate the intent and receive the message when my app is not alive?
there is my code, according to the demo:
public class AgooGcmListenerService extends GcmListenerService{
public final static String TAG = "AgooGcmListenerService";
private AgooFactory agooFactory;
#Override
public void onMessageSent(String msgId) {
super.onMessageSent(msgId);
}
#Override
public void onSendError(String msgId, String error) {
ALog.e(TAG, "onSendError,msgId=" + msgId + ",error=" + error);
super.onSendError(msgId, error);
}
#Override
public void onDeletedMessages() {
super.onDeletedMessages();
}
#Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
ALog.d(TAG, "From: " + from);
ALog.d(TAG, "Message: " + data.toString());
if(TextUtils.isEmpty(message)){
return;
}
byte[] msg = message.getBytes();
Context mContext = getApplicationContext();
agooFactory = new AgooFactory();
agooFactory.init(mContext, null, null);
agooFactory.msgRecevie(msg,
AgooConstants.MESSAGE_SYSTEM_SOURCE_GCM);
Intent intent = new Intent();
intent.setAction("org.agoo.android.intent.action.PING_V4");
intent.setClassName(mContext.getPackageName(),ProxyFactroy.getChannelService(mContext));
intent.putExtra("source", "accs-bundle");
mContext.startService(intent);
}
}
there is my config:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_ADDED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_CHANGED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_INSTALL" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REPLACED" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<service
android:name="org.android.agoo.thirdPush.AgooGcmListenerService"
android:process=":channel"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<!-- [START gcm_receiver] -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:process=":channel"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.taobao.taobao" />
</intent-filter>
</receiver>
<service
android:name="org.android.agoo.thirdPush.AgooInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
Actually my application works fine and able to receive GCM push notifications but there are some of devices which are not receiving GCM notification in GCMIntentService only when my app is in background. devices are huwai honor,xiaomi's some models. So bellow is my code, Let me know if anybody gone through this problem earlier. Can anyone please help me on this?
Android manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.testapp"
android:installLocation="preferExternal"
android:versionCode="41"
android:versionName="4.1.7" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<permission
android:name="com.app.testapp.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.app.testapp.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_LOGS" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<application
android:name="com.app.testapp.MyApplication"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/SampleTheme">
<activity
android:name="com.app.testapp.activity.SplashActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/ActivityTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- GCM -->
<receiver
android:name="com.app.testapp.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.app.testapp" />
</intent-filter>
</receiver>
<!-- Services -->
<service android:name="com.app.testapp.GCMIntentService" />
receiver:
public class GCMBroadcastReceiver extends
com.google.android.gcm.GCMBroadcastReceiver {
#Override
protected String getGCMIntentServiceClassName(Context context) {
return "com.app.testapp.GCMIntentService";
}
}
Service:
package com.app.testapp;
import android.content.Context;
import android.content.Intent;
import com.app.testapp.util.Constants;
import com.app.testapp.util.Logger;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService {
private static String TAG = "GCM";
public MyGCMIntentService() {
super(Constants.SENDER_ID);
}
#Override
protected void onError(Context arg0, String arg1) {
Logger.logger(TAG, "onError");
}
String message = null;
#Override
protected void onMessage(Context context, Intent intent) {
message = intent.getExtras().getString("message");
Logger.logger(TAG, "onMessage message=" + message);
}
#Override
protected void onRegistered(Context arg0, String arg1) {
Logger.logger(TAG, "onRegistered");
}
#Override
protected void onUnregistered(Context arg0, String arg1) {
Logger.logger(TAG, "onUnregistered");
}
}
In the log I am getting this message.
V/GCMBroadcastReceiver(30668): onReceive: com.google.android.c2dm.intent.REGISTRATION
08-07 11:12:59.096: V/GCMBroadcastReceiver(30668): GCM IntentService class: com.example.notifications.GCMIntentService
08-07 11:12:59.096: V/GCMBaseIntentService(30668): Acquiring wakelock
But OnRegistered method is not been invoked ( Running app in my MotoG device ). Below is the manifest.xml coding.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notifications"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<permission android:name="com.example.notifications.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.notifications.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="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.notifications" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
</manifest>
And my MainActivitiy.java file
public static final String SENDER_ID = "xxxxxxxxx";
GCMRegistrar.checkDevice(MainActivity.this);
GCMRegistrar.checkManifest(MainActivity.this);
ChannelUri = GCMRegistrar.getRegistrationId(getApplicationContext());
if(ChannelUri.equals(""))
{
GCMRegistrar.register(getApplicationContext(), SENDER_ID);
}
else
{
Toast.makeText(getApplicationContext(), "Already Registered", Toast.LENGTH_SHORT).show();
}
GCMIntentService.java
public class GCMIntentService extends GCMBaseIntentService {
public static String sRegistrationId;
protected GCMIntentService() {
super(MainActivity.SENDER_ID);
}
public static String getRegistrationId()
{
return sRegistrationId;
}
#Override
protected void onError(Context arg0, String arg1) {
Log.e("Registration", "Got an error!");
Log.e("Registration", arg0.toString() + arg1.toString());
}
#Override
protected void onMessage(Context arg0, Intent intent) {
Log.i("Registration", "Got a message!");
Log.i("Registration", arg0.toString() + " " + intent.toString());
}
#Override
protected void onRegistered(Context arg0, String arg1) {
sRegistrationId = arg1;
Log.i("Registration", "Just registered!");
Log.i("Registration", arg0.toString() + arg1.toString());
}
#Override
protected void onUnregistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
}
Project Structure
Src -> com.example.notifications -> GCMIntentService.java, MainActivity.java
I am trying to integrate GSM in my android app. I am able to run my app and register device through it.
My custom receiver method "GCMReceiver.getGCMIntentServiceClassName" is executed but when GCM tries to instantiate my gcm service (TestService) I get
the following on LogCat:
"Unable to start service Intent { act=com.google.android.c2dm.intent.REGISTRATION flg=0x10 pkg=test.example.app
cmp=test.example.app/com.activate.gcm.GCMIntentService not found"
Any idea?
My activity code:
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
GCMRegistrar.register(getApplicationContext(), "<SENDER_ID>");
Android Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.example.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
<permission android:name="test.example.app.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="test.example.app.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="test.example.app.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="test.example.app.GCMReceiver" 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="test.example.app" />
</intent-filter>
</receiver>
<service android:name=".TestService" android:enabled="true" />
</application>
</manifest>
GCMReceiver class:
package test.example.app;
import android.content.Context;
import android.widget.Toast;
import com.google.android.gcm.GCMBroadcastReceiver;
public class GCMReceiver extends GCMBroadcastReceiver {
#Override
public String getGCMIntentServiceClassName(Context context) {
return "test.example.app.TestService";
}
}
TestService class:
package test.example.app;
public class TestService extends GCMBaseIntentService {
public TestService() {
super("SENDER_ID");
}
#Override
protected void onError(Context arg0, String arg1) {
}
#Override
protected void onMessage(Context arg0, Intent arg1) {
}
#Override
protected void onRegistered(Context arg0, String arg1) {
}
#Override
protected void onUnregistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
}
Try to change TestService name to GCMIntentService (rename it).
I am not very experienced with Android development and I shall include gcm to an existing app.
So far, I can register all devices and I receive the registrationId. The problem is, that I never enter the onMessage() method on my Android 4.0 device. I also tried it with a Galaxy Note 2 (Android 4.1) and there I can receive the message.
I don't get any error message, I just never enter onMessage().
When I send the message, I always get an messageId, so gcm "accepted" the message. But it is never delivered to the device.
How can I find out what the problem is??? Thanks in advance
GCMIntentService.java
public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService() {
super("82........");
}
#Override
protected void onError(Context arg0, String error) {
System.out.println("Not able to register or unregister because: "
+ error);
}
#Override
protected void onMessage(Context context, Intent intent) {
String message = intent.getStringExtra("message");
System.out.println("GCM DELIVERED MESSAGE TO DEVICE:" + message);
}
#Override
protected void onRegistered(Context arg0, String regId) {
System.out.println("Device is registered with RegisterId: " + regId);
}
#Override
protected void onUnregistered(Context arg0, String regId) {
System.out.println("Device with RegisterId: " + regId
+ " has unregistered");
}
#Override
protected boolean onRecoverableError(Context context, String errorId) {
System.out.println("Entered onRecoverableError");
return super.onRecoverableError(context, errorId);
}
}
My Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activities"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<permission
android:name="com.example.activities.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.activities.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 which is called first to decide which Activity comes next -->
<activity
android:name=".StartUp"
android:label="#string/title_activity_start_up" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
android:label="#string/title_activity_login" >
</activity>
<activity
android:name=".DisplayContent"
android:label="#string/title_activity_display_content" >
</activity>
<activity
android:name=".AllSigns"
android:label="#string/title_activity_all_signs" >
</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.activities" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
Code in onCreate() of my first activity (Startup.java)
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
System.out.println("Id ist leer, Device wird neu registriert");
GCMRegistrar.register(this, "825134311331");
} else {
System.out.println("This device is already registered");
}
If you are trying this over WIFI, make sure that port 5228 isn't being blocked by the network. That's how I fixed the problem that I had after more days of trying to get it to work than I'd like to admit.
Make sure the GCMIntentService class is in the package specified in your manifest