Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,209,447 members, 8,006,112 topics. Date: Monday, 18 November 2024 at 04:22 PM

Help Revolved Error In Android Program - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help Revolved Error In Android Program (1322 Views)

Gradle Problem In Android Studio / All You Need To Know As Google Announces Official Support For Kotlin In Android / First Successful Project In Android Studio (2) (3) (4)

(1) (Reply) (Go Down)

Help Revolved Error In Android Program by solacong: 6:26am On Oct 04, 2016
HEllo NLs


Am trying to learn Android programming, though new to programming but i want to learn. Am trying to do an App such that when i send a push notification, and when user click on the notification, it should open an activity with the content of the message sent from FCM.

I have been able to set up push noyication using FCM an it working, but when i clicked on the notification, it always opens "MainActivity.class" even when i have changed the activity in the PendingIntent to the new activity say "Notify.class".

see my code below, please help me out


i have uploaded to google drive



please help, have been on this for weeks
Re: Help Revolved Error In Android Program by Nobody: 6:50am On Oct 04, 2016
One of the key things you must learn as a programmer is how to identify and report errors to other programmers (and also to pin-point the block of codes causing the errors). Learn from the oyinbo programmers on stackoverflow.
You cannot expect a busy programmer to start going all over your codes just to identify an error, who does that?
Re: Help Revolved Error In Android Program by solacong: 7:15am On Oct 04, 2016
Thanks dhtml18,

i think my problems is from the PendingIntent >> notify.class, but am not sure, at least it should open that activity (notify.class) when one clicked on the push notification




package com.new.testnotification;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);


Log.d(TAG, "From: " + remoteMessage.getFrom());

// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}

// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}


}

private void sendNotification(String messageBody) {
Intent intent = new Intent(this, notify.class);
intent.putExtra("p", messageBody);

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("FCM Message"wink
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 , notificationBuilder.build());
}
}
Re: Help Revolved Error In Android Program by Nobody: 7:59am On Oct 04, 2016
Well, what does your logcat show or stacktrace? are you getting any strange errors, nullexception, activity not found exception or whatever?
Re: Help Revolved Error In Android Program by solacong: 3:16pm On Oct 04, 2016
i have not check, cos i don't know how to use the log, but i will find out
Re: Help Revolved Error In Android Program by lekropasky(m): 9:06pm On Oct 04, 2016
Are you sure your notify.class is an Activity/ extends an Activity class?

public class Notify extends AppCompatActivity {

}
Re: Help Revolved Error In Android Program by DonaldGenes(m): 1:41am On Oct 05, 2016
solacong:
HEllo NLs


Am trying to learn Android programming, though new to programming but i want to learn. Am trying to do an App such that when i send a push notification, and when user click on the notification, it should open an activity with the content of the message sent from FCM.

I have been able to set up push noyication using FCM an it working, but when i clicked on the notification, it always opens "MainActivity.class" even when i have changed the activity in the PendingIntent to the new activity say "Notify.class".

see my code below, please help me out


i have uploaded to google drive

https://drive.google.com/drive/folders/0BzxBwUYU-IZNc21YSDdDbWlVTVE?usp=sharing

please help, have been on this for weeks
It is a complex work if iMust add. That involves more coding on the JSOn file that you u must have added on your graddle file
Re: Help Revolved Error In Android Program by solacong: 6:25am On Oct 05, 2016
DonaldGenes:

It is a complex work if iMust add. That involves more coding on the JSOn file that you u must have added on your graddle file


If you are talking about the Json file from FCM, i have done that already, in fact if i send push notification from firebase console, am getting it on my phone, but i just want the notification when clicked to open another activity i called "notify.class" not the "MainActivity"
Re: Help Revolved Error In Android Program by seunthomas: 7:09am On Oct 05, 2016
I checked your zipped code on google drive and am very curious how the code is working.

Can you upload the entire project.

From your code posted here, you are in order.

The issue may therefore be something else thats hidden in your project.
Re: Help Revolved Error In Android Program by solacong: 12:24pm On Oct 06, 2016
seunthomas:
I checked your zipped code on google drive and am very curious how the code is working.

Can you upload the entire project.

From your code posted here, you are in order.

The issue may therefore be something else thats hidden in your project.


MainActivity.java


package com.learncode.testnofication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

MyFirebaseInstanceIDService.java



package com.learncode.testnofication;

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;


public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

private static final String TAG = "MyFirebaseIIDService";

/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
// [START refresh_token]
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);

// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(refreshedToken);
}
// [END refresh_token]

/**
* Persist token to third-party servers.
*
* Modify this method to associate the user's FCM InstanceID token with any server-side account
* maintained by your application.
*
* @param token The new token.
*/
private void sendRegistrationToServer(String token) {
// TODO: Implement this method to send token to your app server.
}
}


MyFirebaseMessagingService.java



package com.learncode.testnofication;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {


// TODO(developer): Handle FCM messages here.
// Not getting messages here? See why this may be: https:///39bRNJ
Log.d(TAG, "From: " + remoteMessage.getFrom());

// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}

// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}

// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
// [END receive_message]

/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, Notificate.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("FCM Message" )
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}


Notificate.java


package com.learncode.testnofication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Notificate extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notificate);
}
}
Re: Help Revolved Error In Android Program by solacong: 12:32pm On Oct 06, 2016
[quote author=seunthomas post=49927647][/quote]

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learncode.testnofication">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.learncode.testnofication.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.learncode.testnofication.Notificate"></activity>

<!-- [START firebase_service] -->
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<!-- [END firebase_service] -->
<!-- [START firebase_iid_service] -->
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<!-- [END firebase_iid_service] -->
</application>

</manifest>


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.learncode.testnofication.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Page"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>


activity_notificate.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.learncode.testnofication.Notificate">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Notificate"
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Re: Help Revolved Error In Android Program by Nobody: 12:50pm On Oct 06, 2016
Una wan kill person with codes from here ni?

(1) (Reply)

Please Assist In Designing This App / Who Knows Excel Macros/vba / Cost Of Installing And Maintaining A Wi-fi Router

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 45
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.